G
Generic Usenet Account
I had a need to tokenize a string, and this is what I came up with. Is
this the most efficient way of tokenizing, or is a more efficient
solution possible?
Thanks,
Gary
#include <iostream>
#include <string>
using namespace std;
main()
{
string::size_type pos;
string cmd="a1::b1::c1::d1";
string subStr;
string token="::";
cout << "cmd string = " << cmd << endl;
try
{
for(pos=0; pos != string::npos
{
pos = cmd.rfind(token);
if (pos != string::npos)
{
cout << "pos = " << pos << '\n';
cmd.erase(pos);
cout << "substring cmd = " << cmd << endl;
}
else
{
throw pos;
}
} // end for
} //end try
catch(string::size_type noPos)
{
cerr << "no more tokens in cmd" << endl;
}
}
this the most efficient way of tokenizing, or is a more efficient
solution possible?
Thanks,
Gary
#include <iostream>
#include <string>
using namespace std;
main()
{
string::size_type pos;
string cmd="a1::b1::c1::d1";
string subStr;
string token="::";
cout << "cmd string = " << cmd << endl;
try
{
for(pos=0; pos != string::npos
{
pos = cmd.rfind(token);
if (pos != string::npos)
{
cout << "pos = " << pos << '\n';
cmd.erase(pos);
cout << "substring cmd = " << cmd << endl;
}
else
{
throw pos;
}
} // end for
} //end try
catch(string::size_type noPos)
{
cerr << "no more tokens in cmd" << endl;
}
}