Effect of noskipws on cin>>

后端 未结 1 1836
余生分开走
余生分开走 2020-12-10 16:44

As I understand, the extraction operator skips the whitespace in the beginning and stops upon encountering a whitespace or end of stream. noskipws can be used to stop ignori

相关标签:
1条回答
  • 2020-12-10 17:00

    The basic algorithm for >> of a string is:

    skip whitespace
    read and extract until next whitespace
    

    If you use noskipws, then the first step is skipped. After the first read, you are positionned on a whitespace, so the next (and all following) reads will stop immediatly, extracting nothing.

    >> to a string will never put whitespace into the string. More generally, using >> with noskipws is problematic, since whitespace is always a separator for >>; it may make sense to use it punctually, but it should generally be reset immediately after it has been used. (The once case where it might make sense is when using >> to a char. In this case, the stream always extracts one character.)

    0 讨论(0)
提交回复
热议问题