有没有办法将<<运算符与字符串流和字符串与空格一起使用?
Is there any way to use << operator with stringstream and strings with spaces?
我在编写我的小解析器时遇到了这个问题,并注意到字符串流在满足空格字符后似乎没有收到更多数据。
基本上
std::stringstream stream;
stream << "Test test";
std::string str;
stream >> str;
std::cout << str;
将打印"测试"而不是"测试测试"。有什么办法可以避免这种情况吗?
是的,使用 std::getline
:
std::string str;
std::getline(stream, str);
std::cout << str; // "Test test"