split string contain special token?

I

indranil b

void tokenise(const string& data, const string& separator, vector<string>& fields)
{
size_t n = data.length();
size_t start = data.find_first_not_of(separator);

while (start < n) {
size_t stop = data.find_first_of(separator, start);
if (stop > n) stop = n;
fields.push_back(data.substr(start, stop-start));
start = data.find_first_not_of(separator, stop+1);
}
}
 
P

Peter Koch Larsen

indranil b said:
void tokenise(const string& data, const string& separator, vector<string>&
fields)
{
size_t n = data.length();
size_t start = data.find_first_not_of(separator);

while (start < n) {
size_t stop = data.find_first_of(separator, start);
if (stop > n) stop = n;
fields.push_back(data.substr(start, stop-start));
start = data.find_first_not_of(separator, stop+1);
}
}

This solution might not work - and causes UB on popular compilers e.g.
Microsoft C++.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top