简体   繁体   中英

C++: Reading from stringstream

I have an istringstream object with string of format STRING,INT,INT,INT eg. "name,20,30,40" I want to read the values into variables of specific types such as std:string and int. How can I do that?

The lazy way:

getline(stream, str, ',');
char c;
stream >> i1 >> c >> i2 >> c >> i3;

It is "lazy" because it does not handle format errors in any sensible way.

The smarter ways would be split on commas into a vector of strings (which can then be converted into integers as needed), or use a full-fledged parser, such as boost.spirit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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