2024年4月28日发(作者:)

在C++有两种字符串流,也称为数组I/O流,一种在 strstream 中定义,另一种

在 sstream 中定义。它们实现的东西基本一样。

strstream里包含:

class strstreambuf;

class istrstream;

class ostrstream;

class strstream;

它们是基于C类型字符串char*编写的

sstream中包含:

class stringbuf;

class istringstream;

class ostringstream;

class stringstream;

它们是基于std::string编写的

因此ostrstream::str()返回的是char*类型的字符串

而ostringstream::str()返回的是std::string类型的字符串

在使用的时候要注意到二者的区别,一般情况下推荐使用std::string类型的字符串

当然如果为了保持和C的兼容,使用strstream也是不错的选择。

但要记住一点,strstream虽仍然是C++语言标准的一部分,但已被C++标准宣称为

“deprecated”,也就是不再提倡使用了,也说不定以后干粹就没了。

先介绍一下sstream

//strigstream流以空格为边界符,使用其须包含sstream头文件

//istringstream 用法

istringstream istring;

string ss("ss 8346520");

(ss);

int i=0;

string s;