istringstream,ostringstream用法

一、
//Example:可以用于分割被空格、制表符等符号分割的字符串

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>  
#include<sstream> //istringstream 必须包含这个头文件
#include<string>
using namespace std;
int main(){
string str="i am a boy";
istringstream is(str);
string s;
while(is>>s) {
cout<<s<<endl;
}
}

输出结果:

i
am
a
boy

二、
//Example:stringstream、istringstream、ostringstream的构造函数和用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <sstream>
int main()
{
// default constructor (input/output stream)
std::stringstream buf1;
buf1 << 7;
int n = 0;
buf1 >> n;
std::cout << "buf1 = " << buf1.str() << " n = " << n << '\n';

// input stream
std::istringstream inbuf("-10");
inbuf >> n;
std::cout << "n = " << n << '\n';

// output stream in append mode (C++11)
std::ostringstream buf2("test", std::ios_base::ate);
buf2 << '1';
std::cout << buf2.str() << '\n';
}

输出结果:

buf1 = 7 n = 7
n = -10
test1

三、
//Example:stringstream的.str()方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <sstream>
#include <iostream>
int main()
{
int n;

std::istringstream in; // could also use in("1 2")
in.str("1 2");
in >> n;
std::cout << "after reading the first int from \"1 2\", the int is "
<< n << ", str() = \"" << in.str() << "\"\n";

std::ostringstream out("1 2");
out << 3;
std::cout << "after writing the int '3' to output stream \"1 2\""
<< ", str() = \"" << out.str() << "\"\n";

std::ostringstream ate("1 2", std::ios_base::ate);
ate << 3;
std::cout << "after writing the int '3' to append stream \"1 2\""
<< ", str() = \"" << ate.str() << "\"\n";
}

输出结果:

after reading the first int from “1 2”, the int is 1, str() = “1 2”
after writing the int ‘3’ to output stream “1 2”, str() = “3 2”
after writing the int ‘3’ to append stream “1 2”, str() = “1 23”

注意事项:

由于stringstream构造函数会特别消耗内存,似乎不打算主动释放内存(或许是为了提高效率),但如果你要在程序中用同一个流,反复读写大量的数据,将会造成大量的内存消耗,因些这时候,需要适时地清除一下缓冲 (用 stream.str(“”) )。
参考链接:

istringstream、ostringstream、stringstream 类介绍 .
https://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html

C++中的 istringstream 的用法
https://blog.csdn.net/jacky_chenjp/article/details/70233212