【问题标题】:Very strange stringstream and std::getline behaviour非常奇怪的 stringstream 和 std::getline 行为
【发布时间】:2012-12-10 07:37:01
【问题描述】:

我正在与引擎中的地图加载错误作斗争,最后我找到了它。但是,似乎 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。

【问题讨论】:

    标签: c++ string stream std getline


    【解决方案1】:

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

    temp = buffer.str();
    

    代替:

    buffer >> temp;
    

    参见:extracting formatted (interpreted) datastringstream.str()

    【讨论】:

    • 有效!非常感谢。如果可能的话,我会接受你的回答。
    • @user1873947 很高兴它有帮助 =)
    猜你喜欢
    • 1970-01-01
    • 2014-07-29
    • 2010-12-17
    • 1970-01-01
    • 2010-12-08
    • 2016-03-13
    • 2011-03-01
    • 1970-01-01
    • 2020-12-02
    相关资源
    最近更新 更多