有没有办法将<<运算符与字符串流和字符串与空格一起使用?

Is there any way to use << operator with stringstream and strings with spaces?

本文关键字:lt 字符串 一起 空格 运算符 有没有      更新时间:2023-10-16

我在编写我的小解析器时遇到了这个问题,并注意到字符串流在满足空格字符后似乎没有收到更多数据。

基本上

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"