非常奇怪的stringstream和std :: getline行为

时间:2012-12-23 14:47:38

标签: c++ string stream std getline

我正在使用我的引擎中的地图加载错误,最后我找到了它。但是,它似乎与stringstream和getline有关...最小代码:

#include <string>
#include <iostream>
#include <sstream>

int main(int argc, char **argv)
{
    std::string text = "1!2!6|6|5|";
    std::stringstream buffer;
    buffer << text; // insert to buffer

    std::string temp;
    buffer >> temp; // extract
    buffer << temp; // then insert again (i need it in my code to count characters)
    // and I can't count it before, as in my code the buffer is filled from file

    std::string loaded;
    std::getline(buffer, loaded, '!'); //should instert "1" to loaded
    std::cout << loaded; //and it displays nothing, debugger shows loaded is empty
}

我做错了什么或是错误吗?我正在使用g ++ 4.7 c ++ 11。

1 个答案:

答案 0 :(得分:4)

在这种情况下,为了从stringstream中提取字符串,您可能需要:

temp = buffer.str();

而不是:

buffer >> temp;

请参阅:extracting formatted (interpreted) datastringstream.str()

相关问题