从std::stringstream中提取整个std::string

Extracting entire std::string From a std::stringstream

本文关键字:std string stringstream 提取      更新时间:2023-10-16

请考虑以下代码:

std::stringstream test;
test << std::string("This is a test.");
std::string str;
test >> str;
std::cout << """ << str << """ << std::endl;

这只输出字符串"This",而不是"This is a test"。

为什么会这样,我怎样才能得到整个字符串?

std::stringstream::str:

std::string str = test.str();