EDIT: This is not a duplicate! The question in the link given is different. I'm not separating by spaces, I'm separating by anything other than characters in a given string. Therefor, the answers in the other thread don't work.
EDIT2: Please note that I'm not given the delimiters to separate by, I need to separate by anything OTHER THAN notOf
.
In my program, that means cut up string named word into strings in a vector named words, which consist of and are separated by anything other than notOf
. So notOf
is sort of the opposite of a delimited in getline
.
For example, "&otherwise[taken=(and)-redacted-"
should become "otherwise"
, "taken"
, "and"
and "redacted"
.
I've tried writing and came up with what you see below, but it doesn't work and is also, I've been told, considerably ugly. Use your judgement whether it is worth fixing or writing anew.
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
string notOf = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789$'\"";
string word = "&otherwise[taken=(and)-redacted-";
stringstream in(word);
vector<string> words;
if (word.find_first_not_of(notOf) == string::npos)
words.push_back(word);
else while (word.find_first_not_of(notOf) != string::npos)
{
int index = word.find_first_not_of(notOf);
string tok;
getline(in, tok, word[index]);
if (index != 0)
words.push_back(tok);
word = word.substr(index);
}
for (vector<string>::iterator it = words.begin(); it != words.end(); ++it)
cout << *it << endl;
system("pause");
return 0;
}