ostringstream类向一个string插入字符,istringstream类从一个string对象读取字符,而stringstream类可以用来支持读和写两种操作。为了使用string流,我们必须包含相关的头文件: #include <sstream> 例如, string read_file_into_string() { ifstream ifile( "alice_emma" ); ostringstream buf; char ch; while(buf && ifile.get( ch )) buf.put( ch ); return buf.str(); } 成员函数str()返回与ostringstream类对象相关联的string对象。 istringstream由一个string对象构造而来,它可以读取该string对象。istringstream的一种用法是将数值字符串转换成算术值。 ---摘抄自《C++PRIME》.

评论