#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <typeinfo>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
int n;
istringstream in; // 亦可使用 in("1 2")
in.str("1 2 3 4 5");
in >> n;
cout << n << ", str() = " << in.str() << endl;
in >> n;
cout << n << ", str() = " << in.str() << endl;
in.clear(); //不能实现清零,作用是:修改状态标志
in >> n;
cout << n << ", str() = " << in.str() << endl;
in.str(""); //可以实现清空
cout << "清空后:" << in.str() << endl;
//---------------
cout << "------------------------------------" << endl;
ostringstream out("1 2");
out << 3;
cout << "after writing the int '3' to output stream \"1 2\""
<< ", str() = \"" << out.str() << "\"\n";
ostringstream ate("1 2", std::ios_base::ate);
ate << 3;
cout << "after writing the int '3' to append stream \"1 2\""
<< ", str() = \"" << ate.str() << "\"\n";
return 0;
}
输出:
1, str() = 1 2 3 4 5
2, str() = 1 2 3 4 5
3, str() = 1 2 3 4 5
清空后:
------------------------------------
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"