Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 非常奇怪的stringstream和std::getline行为_C++_String_Stream_Std_Getline - Fatal编程技术网

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

C++ 非常奇怪的stringstream和std::getline行为,c++,string,stream,std,getline,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

我正在与引擎中的地图加载错误搏斗,最终我找到了它。然而,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
}
#包括
#包括
#包括
int main(int argc,字符**argv)
{
std::string text=“1!2!6 | 6 | 5 |”;
std::stringstream缓冲区;
buffer>temp;//提取

buffer用于从stringstream提取字符串在这种情况下,您可能需要:

temp = buffer.str();
而不是:

buffer >> temp;

请参阅:vs.

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

temp = buffer.str();
而不是:

buffer >> temp;

请参阅:vs.

它有效!非常感谢。如果可能,我会接受你的答案。@user1873947高兴它有帮助=)它有效!非常感谢。如果可能,我会接受你的答案。@user1873947高兴它有帮助=)