您的位置:首页 > 其它

I/O流类库(四)

2016-01-05 15:39 218 查看

字符串流

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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: