头文件:Defined in header
IO类之stringstream
也是一个类模板
stringstream的对象与内存中的string对象建立关联, 往string对象写东西, 或者从string对象读取东西
istringstream的关联是ok的, 但是ostringstream不能依赖于关联
#include <sstream>
#include <iostream>
int main()
{
int n;
std::istringstream in; // could also use in("1 2")
in.str("1 2");
in >> n; //i 是读出
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; //o 是读入
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";
}
in 是将里面的东西可以读出》》
out 是将外面的东西读入《《
两者都有成员函数 .str() 输出其中的字符串
并且注意:
- out读入的时候,可以直接读入int类型的数据,因为已经重载过了。/没有指定后面有参数ate时,是在最开始位置覆盖。并且注意是字符串。
- 如果std::ostringstream out(“1 2”, std::ios_base::ate); 有定义的话,是从最后面加入
- in 关联的时候