Hey so I am little confused on how delimiter work. I need write a function that splits the string by delimiter. Function header is:
vector<string> split(stirng target, string delimiter);
for example, the code inside main calling your function
vector<string> v = split("hiAAAmyAAAnameAAAis", "AAA");
and v should come out as hi my name is
So is it something like
vector<string> split(string target, string delimiter)
{
vector<string> word;
string s = "hiAAAmyAAAnameAAAis";
string delimiter = "AAA";
#include <string>
#include <vector>
#include <iostream>
usingnamespace std;
vector<string> split_str(const string& str, const string& delimeter){
vector<string> result;
string::const_iterator begin, str_it;
// Point to the first character in the input string.
str_it = str.begin();
// Get to the first non-delimit character in string.
while (delimeter.find(*str_it) != string::npos && str_it != str.end())
str_it++;
// Make "begin" point to the first non-delimit character in string.
begin = str_it;
do {
// Find position of first delimit character in str
while (delimeter.find(*str_it) == string::npos && str_it != str.end())
str_it++;
string token = string(begin, str_it); // make a copy of the valid token.
result.push_back(token); // push it into the vector
// Skip through the additional consecutive delimit characters
while (delimeter.find(*str_it) != string::npos && str_it != str.end())
str_it++;
// reset the begin to the start of the remaining text.
begin = str_it;
} while (str_it != str.end());
return result;
}
int main()
{
vector<string> v = split_str("hiAAAmyAAAnameAAAis", "AAA");
for (vector<string>::const_iterator it = v.begin(); it != v.end(); it++)
cout << *it << "\n";
return 0;
}