Search code examples
c++stringstream

stringStream functions


Morgan 201128374 4383745,12394 5455.30
drew  22223        91939,5324 55.9
stringstream strm;
sstream strm(s)
strm.str() return a copy of a string
strm.str(s) copies the string into strm return void.

I am looking for more function when I assign a variable to an Stringstream. How would I ignore punctuation mark? My book only listed those two function above.

struct PersonInfo{
string name;
vector<string> numbers;
}
string line, word;
vector<PersonInfo> people;
while(getline(cin, line))
{
    PersonInfo Info;
    istringstream record(line);
    record >> info.name;
    while (record >> word)
       info.numbers.push_back(word);
    people.push_back(info);
}

Solution

  • How would I ignore punctuation mark? My book only listed those two function above.

    Use std::getline, which takes in a separator argument:

    if(! std::getline(record, line, ' ') ) // reads to first space
        throw std::runtime_error{ "bad stream format" };
    

    You should read to a separator, and alternate the separator to be either ' ' or ';'.