So I am just trying to learn more about strings and I am having trouble with npos, I cant seem to visualize or grasp what its doing, I think I might know but I am unsure, please correct me if im wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
using namespace std;
int main()
{
string one, two;
cin >> one;
string::size_type pos = one.find(':');
if(one.npos != pos)
{
two = one.substr(pos + 1);
one = one.substr(0, pos);
}
cout << one << endl;
cout << two << endl;
return 0;
}
|
string::size_type pos = one.find(':');
So the .find returns the position it found the character at, in this case ":", and string::size_type pos stores that value in it, correct?
if(one.npos != pos)
and this is saying if the position of the string is not equal to the position of the ":" character, then do the code in the body.
two = one.substr(pos + 1);
one = one.substr(0, pos);
Im a little confused at these though. I think the second line resets the position?