0

I have a piece of code that currently splits a string using a whitespace as a delimiter. Each token is stored collectively in a vector of strings.The split function is show below:

vector<string> split(const string &s, char delim) {
    stringstream ss(s);
    string item;
    vector<string> tokens;
    while (getline(ss, item, delim)) {
        tokens.push_back(item);
    }
    return tokens;
}

The vector is defined as such:

vector<string> mocapVector = split(buffer.str(), ' ');

Example output of the vector can be seen here, where an item of the vector is outputted, and the console is shown alongside the original data file (the entire string before it is split.

To reiterate:

a b c
d e f
g h i

gets outputted as:

a

b

c
d

e

f
g

h

i

In the example, only the whitespace is a delimiter, and not CRLF (end of line). My question is, how can I use both whitespace AND CRLF as delimiters simultaneously, so that each string in the vector is a single token, and not clumps of tokens together?

Ideally would like to avoid using external libraries if at all possible.

Thank you!

EDIT: Solution has been found thanks to @mindriot and @πάνταῥεῖ .

The solution is:

vector<string> split(const string &s, char delim) {
        stringstream ss(s);
        string item;
        vector<string> tokens;
        while (ss >> item) {
            tokens.push_back(item);
        }
        return tokens;
    }

Thanks to all who responded!

User499
  • 21
  • 8

0 Answers0