ostringstream是将数据写入string里边的,istringstream是将从string里边读出数据的;
#include <sstream> int main() { std::ostringstream ostr; while(std::cin) { std::string str; std::cout << "next word :"; std::cin >> str; if(str == "done") { break; } ostr << str << " "; } std::cout << "The result is : " << ostr.str() << std::endl; return 0; }
输入:
a
bc
d
ef
done
结果是:
a bc d ef