字符串流
istringstream,由istream派生而来,提供读string的功能 ostringstream,由ostream派生而来,提供些string的功能 stringstream,由iostream派生而来,提供读写string的功能
istringstream
#include <iostream>
#include<sstream>
using namespace std ;
int main(void )
{
string line;
string word;
while (getline(cin , line))
{
istringstream iss(line);
while (iss >> word)
cout << word << "#" ;
cout << endl;
}
}
ostringstream
#include <iostream>
#include<sstream>
using namespace std ;
string Double2Str(double & val)
{
ostringstream oss;
oss << val;
return oss.str();
}
double Str2Double(const string & str)
{
istringstream iss(str);
double val;
iss >> val;
return val;
}
int main(void )
{
double val = 55.55 ;
string str = Double2Str(val);
cout << str << endl;
str = "123.123" ;
val = Str2Double(str);
cout << val << endl;
return 0 ;
}
#include <iostream>
#include<sstream>
using namespace std ;
int main(void )
{
istringstream iss("192,168,0,100" );
cout << iss.str() << endl;
int v1, v2, v3, v4;
char ch;
iss >> v1 >> ch >> v2 >> ch >> v3 >> ch >> v4;
ostringstream oss;
ch = '.' ;
oss << v1 << ch << v2 << ch << v3 << ch << v4;
cout << oss.str() << endl;
return 0 ;
}