I am new to C++ and I am having trouble splitting a string by a delimiter and putting the substrings into a vector.
My code is as follows:
vector<string> split(const string &s, const string &delim)
{
string::size_type pos = s.find_first_of(delim,0);
int start = 0;
vector<string> tokens;
while(start < s.size())
{
if(start++ != pos + 1)
tokens.push_back(" ");
pos = s.find_first_of(delim, start);
tokens.push_back(s.substr(start, pos - start));
}
for(vector<string>::size_type i = 0; i != tokens.size(); ++i)
cout << tokens[i];
return tokens;
}
a string and a delimiter are passed into the function and and performs the splitting. This function is suppose to put empty strings into the vector but does not do it for me.
for example if I call the function in main as:
int main()
{
split("<ab><>cd<", "<>");
}
the output is suppose to be
"","ab","","","","cd",""
minus the quotes
but the output for my code currently is
ab b cd d
any help would be appreciated.