当前位置:知之问问>百科知识>c++ stringstream

c++ stringstream

2023-08-09 13:12:29 编辑:join 浏览量:565

c++ stringstream

stringstream 是字符串流,相当于一个大的缓冲区,很多类型的数据都可以存储,也可以从中读出,就像文件流一样。

stringstream ss(s);//定义了一个字符串流,并用一个字符串初始化

ss << i;// 将i读入到ss中去

cout <

stringstream的构造函数原形如下:

stringstream::stringstream(string

str);

#include

#include

#include

using

namespace

std;

int

main()

{

stringstream

ostr("ccc");

ostr.put('d');

ostr.put('e');

ostr<<"fg";

string

gstr

=

ostr.str();

cout<

char

a;

ostr>>a;

cout<

system("pause");

}

用在类型转化的一个例子:

#include

#include

#include

int

main()

{

std::stringstream

stream;

std::string

result;

int

i

=

1000;

stream

<<

i;

//将int输入流

stream

>>

result;

//从stream中抽取前面插入的int值

std::cout

<<

result

<<

std::endl;

//

print

the

string

"1000"

}

另外有istringstream和ostringstream之分,其实用法和fstream的ofstream

ifstream

iostream的istream

ostream等类是非常相似的。任何一个介绍i/o标准库或者stl的书都有详细的介绍。

给你推荐一个网站吧

标签:c++,stringstream

版权声明:文章由 知之问问 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.zhzhwenwen.com/article/210650.html
热门文章