boom boom - 1 month ago 5
C++ Question

What does string::npos mean

What does the statement

string::npos
mean here

found=str.find(str2);

if (found!=string::npos)
cout << "first 'needle' found at: " << int(found) << endl;

Answer

It means not found.

It is usually defined like so:

static const size_t npos = -1;

It is better to compare to npos instead of -1 because the code is more legible.

Comments