cin can read a white-spaced token string.

    1. int a, b, c;
    2. cin >> a >> b >> c;
    3. //> 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.

    1. string line, word; // will hold a line and word from input, respectively
    2. vector<PersonInfo> people; // will hold all the records from the input
    3. // read the input a line at a time until cin hits end-of-file (or another error)
    4. struct PersonInfo {
    5. string name;
    6. vector<string> phones;
    7. };
    8. while (getline(cin, line)) {
    9. PersonInfo info; // create an object to hold this record’s data
    10. istringstream record(line); // bind record to the line we just read
    11. record >> info.name; // read the name
    12. while (record >> word) // read the phone numbers
    13. info.phones.push_back(word); // and store them
    14. people.push_back(info); // append this record to people
    15. }