I was practicing about cutting a string into parts, and I wrote these codes.
string a = "12/1322.39102";
int current = 0;
int next;
while (true)
{
next = a.find_first_of("/.", current);
if (next != current)
{
cout << "c = " << current << endl;
cout << "n - c = " << next-current << endl;
string tmp = a.substr(current, next-current);
if (tmp.size())
cout << tmp << endl;
}
if (next == string::npos)
break;
current = next + 1;
}
I was confused because if find_first_of
cannot find any character (i.e. '/' and '.'), it'll return npos
, or return -1.
In that case, I think it'll not output the last number 39102 because next-current equal -9.
Another question is, what's the difference between npos
and end()
(used in vector or map) ?