What does string::npos mean in this code?

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.


string::npos is a constant (probably -1) representing a non-position. It's returned by method find when the pattern was not found.


The document for string::npos says:

npos is a static member constant value with the greatest possible value for an element of type size_t.

As a return value it is usually used to indicate failure.

This constant is actually defined with a value of -1 (for any trait), which because size_t is an unsigned integral type, becomes the largest possible representable value for this type.