数据的接收与转换

C/C++7个月前更新 3153917921
28 0 0
  1. 将string型的数字转化为int型的数字
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string str;
        cin>>str;
        int b = str[0]-'0';
    }
  2. 从数字与符号混合的string中分离出int类型的数字
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string str;
        cin>>str;
        for(int i=0;i<str.size();i++)
        {
            int j=i,x=0;
            if(isdigit(str[i]))
            {
                while(isdigit(str[j]) && j<str.size())
                {
                    x=x*10+str[j++]-'0';
                }
                i=j-1;
                cout<<x;//每一个x都是一个int类型的数字
            }
        }
    }

     

© 版权声明

相关文章

暂无评论

暂无评论...