简体   繁体   English

stringstream >>运算符

[英]stringstream >> operator

I have this code: 我有这个代码:

    std::string s = "\n\n\n\n+\n\n\n+";
    std::stringstream serializedDataStream(s);

    std::string plusCharacter, plusCharacter2;
    serializedDataStream >> plusCharacter ;
    cout << "plusCharacter "<<plusCharacter << "\n";
    serializedDataStream >> plusCharacter2 ;
    cout << "plusCharacter "<<plusCharacter2;

    //OUTPUT:
    //    plusCharacter +
    //    plusCharacter +

which means that the stringsteam >> operator skipped new lines. 这意味着stringsteam >>运算符跳过了新行。 I looked into the std::stringstream documentation but I couldn't find an explanation for why this is happening. 我查看了std::stringstream文档但我无法找到解释为什么会发生这种情况的原因。 Is this a compiler specific behavior, or can I rely on this? 这是编译器特定的行为,还是我可以依赖于此?

This occurs because operator>> behaves as a formatted input function . 这是因为operator>>表现为格式化的输入函数 Part of the process of a formatted input function is: 格式化输入函数的部分过程是:

Each formatted input function begins execution by constructing an object of class sentry with the noskipws (second) argument false. 每个格式化的输入函数通过使用noskipws (第二个)参数false构造类哨兵的对象来开始执行。

When noskipws is set to false (and the skipws flag on the stream being true, which is default), the sentry object "extracts and discards each character as long as the next available input character c is a whitespace character." noskipws设置为false(并且流上的skipws标志为true,这是默认值)时,只要下一个可用输入字符c是空白字符, sentry对象“就会提取并丢弃每个字符”。

If you want to read each line at a time, use std::getline . 如果要一次读取每一行,请使用std::getline This function behaves as an unformatted input function which has noskipws set to true and reads a line of text (as defined by the line terminator (parameter 3 of std::getline() (which defaults to '\\n'))). 此函数表现为无格式输入函数 ,其noskipws设置为true并读取一行文本(由行终止符定义(std :: getline()的参数3(默认为'\\ n')))。

Read up here. 在这里阅读。

Behaves as an FormattedInputFunction. 表现为FormattedInputFunction。 After constructing and checking the sentry object, which may skip leading whitespace , extracts successive characters and stores them at successive locations of a character array whose first element is pointed to by s. 在构造和检查可以跳过前导空格的哨兵对象之后,提取连续的字符并将它们存储在由s指向其第一个元素的字符数组的连续位置处。

So as per the standard, it will toddle along whatever is in the stream until it actually finds something interesting, read that back to you, then stop. 因此,按照标准,它将沿着流中的任何东西蹒跚学步,直到它实际发现一些有趣的东西,然后读回给你,然后停止。 Then do the same again next time. 然后下次再做同样的事情。

std::getline on the other hand will read everything up to the next newline character (this will be pulled off the stream but not given back to you) and gives it all back to you in a std::string . 另一方面, std::getline将读取下一个换行符的所有内容 (这将从流中删除但不会返回给你)并在std::string中将它全部返回给你。 It is then up to you to get what you want (eg a number) from that string . 然后由你来得到你想要的东西从(如数字) string

Edit: I'm struggling to find exactly which characters are considered to be whitespace by the default locale. 编辑:我正在努力通过默认语言环境确切地找到哪些字符被认为是空格。 I think it will be the same as defined for isspace (inherited from c), which are space (0x20), form feed (0x0c), line feed (0x0a), carriage return (0x0d), horizontal tab (0x09) and vertical tab (0x0b). 认为它将与为isspace (继承自c)定义的相同,即space (0x20), form feed (0x0c), line feed (0x0a), carriage return (0x0d), horizontal tab (0x09) and vertical tab (0x0b). But I haven't been able to convince myself 100% it is the same in this case. 但是我无法说服自己100%在这种情况下它是一样的。 I'm not good with locales. 我不熟悉语言环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM