129

What does the phrase std::string::npos mean in the following snippet of code?

found = str.find(str2);

if (found != std::string::npos)
    std::cout << "first 'needle' found at: " << int(found) << std::endl;
einpoklum
  • 118,144
  • 57
  • 340
  • 684
boom
  • 5,856
  • 24
  • 61
  • 96

13 Answers13

134

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.

tronman
  • 9,862
  • 10
  • 46
  • 61
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • 6
    Comparing == -1 might make also make some people think they can convert that into < 0 which is NOT the same thing and will not work. – Andy Dent Mar 26 '12 at 07:56
  • Just wondering if anyone has come across this, or is it just me...I run `cout<<"pos: "< – user1135469 Apr 10 '13 at 14:13
  • @user1135469 if you see the answer of codaddict bellow (http://stackoverflow.com/a/3827997/752842) or of Sebastian Raschka, I think what you are getting will make sense. And I would recommend using npos, because I tried using -1 and it was not working properly under the conditions I was using it. – Dzyann Nov 19 '13 at 16:17
66

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

Sheldon L. Cooper
  • 3,230
  • 19
  • 13
  • 21
    +1 for actually showing the npos = no-pos derivation that makes it easy to remember. It's so obvious you wouldn't think about it once you knew it, but for someone seeing those letters for the first time it may not click...? – Tony Delroy Sep 30 '10 at 07:51
  • 7
    wrong on 47 levels... npos is of size_t, it means it can not be negative... real meaning is max_index, 18446744073709551615 for 64 bit size_t – NoSenseEtAl Sep 04 '14 at 12:33
28

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.

Community
  • 1
  • 1
codaddict
  • 445,704
  • 82
  • 492
  • 529
23

size_t is an unsigned variable, thus 'unsigned value = - 1' automatically makes it the largest possible value for size_t: 18446744073709551615

  • 1
    size_t is unsigned int for 32 bit compiler; unsigned long long int for 64 bit compiler.. Setting it to -1 makes it have the max val of that unsigned type. – sudheerbb Jan 21 '20 at 10:26
11

std::string::npos is implementation defined index that is always out of bounds of any std::string instance. Various std::string functions return it or accept it to signal beyond the end of the string situation. It is usually of some unsigned integer type and its value is usually std::numeric_limits<std::string::size_type>::max () which is (thanks to the standard integer promotions) usually comparable to -1.

wilx
  • 17,697
  • 6
  • 59
  • 114
5

we have to use string::size_type for the return type of the find function otherwise the comparison with string::npos might not work. size_type, which is defined by the allocator of the string, must be an unsigned integral type. The default allocator, allocator, uses type size_t as size_type. Because -1 is converted into an unsigned integral type, npos is the maximum unsigned value of its type. However, the exact value depends on the exact definition of type size_type. Unfortunately, these maximum values differ. In fact, (unsigned long)-1 differs from (unsigned short)-1 if the size of the types differs. Thus, the comparison

idx == std::string::npos

might yield false if idx has the value -1 and idx and string::npos have different types:

std::string s;
...
int idx = s.find("not found"); // assume it returns npos
if (idx == std::string::npos) { // ERROR: comparison might not work
...
}

One way to avoid this error is to check whether the search fails directly:

if (s.find("hi") == std::string::npos) {
...
}

However, often you need the index of the matching character position. Thus, another simple solution is to define your own signed value for npos:

const int NPOS = -1;

Now the comparison looks a bit different and even more convenient:

if (idx == NPOS) { // works almost always
...
}
Debashish
  • 1,155
  • 19
  • 34
5

found will be npos in case of failure to find the substring in the search string.

dalle
  • 18,057
  • 5
  • 57
  • 81
Raghuram
  • 3,937
  • 2
  • 19
  • 25
1
$21.4 - "static const size_type npos = -1;"

It is returned by string functions indicating error/not found etc.

Chubsdad
  • 24,777
  • 4
  • 73
  • 129
1

static const size_t npos = -1;

Maximum value for size_t

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

This value, when used as the value for a len (or sublen) parameter in string's member functions, means "until the end of the string".

As a return value, it is usually used to indicate no matches.

This constant is defined with a value of -1, which because size_t is an unsigned integral type, it is the largest possible representable value for this type.

Leninkumar
  • 314
  • 3
  • 3
1

An answer for these days of C++17, when we have std::optional:

If you squint a bit and pretend std::string::find() returns an std::optional<std::string::size_type> (which it sort of should...) - then the condition becomes:

auto position = str.find(str2);

if ( position.has_value() ) {
    std::cout << "first 'needle' found at: " << position.value() << std::endl;
}
einpoklum
  • 118,144
  • 57
  • 340
  • 684
1

Value of string::npos is 18446744073709551615. Its a value returned if there is no string found.

Ayush ShaZz
  • 316
  • 3
  • 7
  • 1
    The actual value is implementation defined and irrelevant. In practice, however, the value `18446744073709551615` would be typical for 64-bit `std::size_t`, it is maximum 64-bit unsigned value. – Alex Guteniev Apr 20 '20 at 17:49
1

As others have mentioned, string::npos it's the maximum value for size_t.

Here is its definition:

static constexpr auto npos{static_cast<size_type>(-1)};

Puzzled that the wrong answer got the vote.

And here is a quick testing sample:

int main()
{
    string s = "C   :";
    size_t i = s.rfind('?');
    size_t b = size_t (-1);
    size_t c = (size_t) -1;
    cout<< i <<" == " << b << " == " << string::npos << " == " << c;

    return 0;
}

output:

18446744073709551615 == 18446744073709551615 == 18446744073709551615 == 18446744073709551615

...Program finished with exit code 0
Genci Ymeri
  • 104
  • 10
0

I too was wondering what npos was when I was using substr(), since npos is the default length of the substring, as per:

string substr (size_t pos = 0, size_t len = npos) const;

The important thing for me to note was that the substring will not be longer than the length of the original string, which is why a len of npos works, as per:

The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first).

Will
  • 1,509
  • 1
  • 13
  • 16