小编Tec*_*Guy的帖子

C++:使用std :: istream或sentry环绕的奇怪行为

这个小的自定义getline函数是作为一个关于处理不同行结尾的问题的答案.

该功能非常有效,直到2天前编辑,使其不会跳过每行的前导空格.但是,在编辑之后,程序现在进入无限循环.对代码进行的唯一更改是以下更改的行:

std::istream::sentry se(is);  // When this line is enabled, the program executes
                              // correctly (no infinite loop) but it does skip
                              // leading white spaces
Run Code Online (Sandbox Code Playgroud)

对此:

std::istream::sentry se(is, true); // With this line enabled, the program goes 
                                   // into infinite loop inside the while loop  
                                   // of the main function.
Run Code Online (Sandbox Code Playgroud)

如果我们指定不跳过空格,有人可以帮我解释为什么程序无限循环?

这是完整的程序......

std::istream& safeGetline(std::istream& is, std::string& t)
{
    t.clear();

    // The characters in the stream are read one-by-one using a std::streambuf.
    // That is faster than reading …
Run Code Online (Sandbox Code Playgroud)

c++ istream sentry

3
推荐指数
1
解决办法
1644
查看次数

标签 统计

c++ ×1

istream ×1

sentry ×1