1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void tokenize(const string& str,vector<string>& tokens, const string& delimiter)
{
//string:: size_type lastPos = str.find_first_not_of(delimiter, 0);
int lastPos = str.find_first_not_of(delimiter, 0);
cout<<"last position is\t"<<lastPos<<endl;
string:: size_type pos = str.find_first_of(delimiter, lastPos);
cout<< "position is\t"<<pos<<endl;
while(string::npos !=lastPos || string::npos != pos)
{
cout<<"enter into while loop\t";
tokens.push_back(str.substr(lastPos, pos-lastPos));
cout<<"token is\t"<<tokens[pos]<<endl;
cout<<"pos-lastPos is\t"<<(pos-lastPos)<<endl;
lastPos = str.find_first_not_of(delimiter, pos);
pos = str.find_first_of(delimiter, lastPos);
}
return;
}
int main()
{
vector<string> tokens;
string numbers("10 60 30, 59, 55, 22, 40 90, 15");
tokenize(numbers,tokens, ",");
vector<string> :: iterator it;
cout<<"Tokens contain:\t";
it=tokens.begin();
while(it !=tokens.end())
{
cout<<" "<<*it<<endl;
it++;
}
return 0;
}
|