首页 > 代码库 > c++的字符串流

c++的字符串流

 整型数据

#include <iostream>#include <string>#include <sstream>using namespace std;int main( ){    string s;    int x;    int sum;    while (getline(cin, s))    {        stringstream ss(s);        sum=0;        while (ss>>x) sum=sum+x;        cout<<sum<<endl;    }    return 0;}
View Code

 

 

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main( )
{
    string s;
    int x;                                                    //整数
    int sum;
    while ( getline(cin, s) )
    {
        stringstream ss(s);                       //字符串流 初始化                       stringstream ss=s ;            
        sum=0;
        while (ss>>x)                                 //ss   导入到 输入到   中 

                     sum=sum+x;           // sum  进行统计      
        cout<<sum<<endl;
    }
    return 0;
}

 

 

 

 

 

*******************************************************************************************************************************************

字符串类型

 

 

 

#include <iostream>#include <string>#include <sstream>using namespace std;int main( ){    string s,s2;    while (getline(cin, s))    {        stringstream ss(s);        while (ss>>s2)            cout<<s2<<endl;    }    return 0;}
View Code

 

 

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main( )
{
    string s,s2;
    while (getline(cin, s))
    {
        stringstream ss(s);
        while (ss>>s2)
            cout<<s2<<endl;
    }
    return 0;
}

 

 

 

*************************************************************************************************************************************