摘自:http://www.3gtarena.com/danapeixunjishu/C__peixun/2048.html
今天看到了一个比较有用的c++的输入输出控制类。C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含
istringstream类用于执行C++风格的串流的输入操作。
ostringstream类用于执行C风格的串流的输出操作。
strstream类同时可以支持C风格的串流的输入输出操作。
istringstream的构造函数原形如下:
istringstream::istringstream(string str);
它的作用是从string对象str中读取字符。
View Code
#include<iostream>
#include<sstream> //istringstream 必须包含这个头文件
#include<string>
using namespace std;
int main()
{
string str=“i an a boy”;
istringstream is(str);
string s;
while(is》s)
{
cout《s《endl;
}
}
输出是:
i
am
a
boy
摘自:http://www.cppblog.com/shyli/archive/2006/10/17/13758.html
View Code
istringstream对象可以绑定一行字符串,然后以空格为分隔符把该行分隔开来。
#include<iostream>
#include<sstream>
using namespace std;
int main()
{
string str, line;
while(getline(cin, line))
{
istringstream stream(line);
while(stream>>str)
cout<<str.c_str()<<endl;
}
return 0;
}
测试:
input:
abc df e efgeg ffg
ouput:
abc
df
e
efgeg
ffg
原文链接: https://www.cnblogs.com/KeenLeung/archive/2013/03/18/2966447.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/81023
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!