Stringstream extract integer

后端 未结 4 518
小鲜肉
小鲜肉 2021-01-04 18:27

Why do I fail to extract an integer value into the Num variable?

#include 
#include 
#include 

usi         


        
4条回答
  •  失恋的感觉
    2021-01-04 19:15

    You have to reset all status flags (eofbit) and bring the stream into a good state (goodbit):

    ss.clear(); // clear status flags
    ss.str(Tokens[0]);
    

    The reason is that if you keep extracting until the end, you will hit the end, and the eof flag will be set on that stream. After that, read operations will be canceled and you have to clear that flag out again. Anyway, after clearing and resetting the string, you can then go on extracting the integers.

提交回复
热议问题