字符串流不接受空格?

stringstream doesn't accept white space?

本文关键字:空格 不接受 字符串      更新时间:2023-10-16

我有以下代码:

std::stringstream ss;
ss << 1 << "a b c";
std::string result;
ss >> result;
std::cout << result << std::endl;

我看到"1a"而不是"1a b c"。

我在某个地方读到,我应该有ss <<std:: noskip。但这没有用。

任何想法?

std::getline(ss, result);

或者直接取string

result = ss.str();
//Try using this for getting whitespace in string
    string input;
    cout<<"nInput : "<<input;
    getline(cin,input);
    string result,label;
    std::stringstream sstr(input);
    while(sstr>>label){
        result=result+label+" ";
    }
cout<<"nResult : "<<result;