Splitting a string into integers using istringstream in C++

后端 未结 2 1313
难免孤独
难免孤独 2020-12-23 22:35

I\'m trying to use istringstream to split a simple string into a series of integers:

#include 
#include 
#include          


        
2条回答
  •  时光说笑
    2020-12-23 23:09

    Hope this helps:
    iss : 1 2 3
    Iteration 1
    iss : 1 2 3 (Initially)
    n=1
    iss : 2 3
    //* 1 is printed
    Iteration 2:
    iss : 2 3 (Initially)
    n=2
    iss : 3
    //* 2 is printed
    Iteration 3
    iss : 3
    n=3
    iss : ''
    Iteration 4
    iss : ''
    n not changed//Flag set for eof of iss as no further input from stream
    iss : ''

    And as rightly mentioned by the above post while (iss) is not dissimilar from while (iss.eof()).
    Internally, the function(istream::operator>>) accesses the input sequence by first constructing a sentry object (with noskipws set to false[This means that space is separator and your list will be 1,2,3]). Then (if good[here eof not reached]), it calls num_get::get [Get the next integer] to perform both the extraction and the parsing operations, adjusting the stream's internal state flags accordingly. Finally, it destroys the sentry object before returning.

    Refer : http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/

提交回复
热议问题