cin can read a white-spaced token string.
int a, b, c;cin >> a >> b >> c;//> 1 2 7
stringstream is a stream class to operate on strings. It basically implements input/output operations on memory (string) based streams. stringstream can be helpful in different type of parsing. The following operators/functions are commonly used here
Operator >> Extracts formatted data. It returns true is extracts success, and false if no more content inside
Operator << Inserts formatted data.
Method str() Gets the contents of underlying string device object.
Method str(string) Sets the contents of underlying string device object.
Its header file is sstream.
string line, word; // will hold a line and word from input, respectivelyvector<PersonInfo> people; // will hold all the records from the input// read the input a line at a time until cin hits end-of-file (or another error)struct PersonInfo {string name;vector<string> phones;};while (getline(cin, line)) {PersonInfo info; // create an object to hold this record’s dataistringstream record(line); // bind record to the line we just readrecord >> info.name; // read the namewhile (record >> word) // read the phone numbersinfo.phones.push_back(word); // and store thempeople.push_back(info); // append this record to people}
