开发者

Behavior of the C++ extraction operator when used to read into a string

I was w开发者_JAVA技巧orking with some C++ code, and noticed some code of the following form:

ss >> str;

where ss is a stream (a stringstream, in this case), and str is a string.

What is the defined behavior of this code? Specifically, what is the value of str after this is executed?


Unless the skipws flag is set in ss.flags() (it is by default, but you can unset it), white space is skipped (and not copied into str), then ss copies text from the input until either a white space or end of file is encountered (or it runs out of memory, or it reads std::string::max_size characters).

What is white space is determined by the ctype<char> locale imbued in ss.


The >> operator will read up to the first whitespace character (or the end of the stream) and assigning them to str.

If your input stream contained "ab cde". Then str will equal "ab".


The expected behavior is that the first word stored in ss will be consumed and stored in str, where word is the longest sequence of non-space characters according to the locale obtained after removing any leading space character from ss.


It puts the bytes from ss to str.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜