What does string::npos mean in this code?

156,657

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
156,657

Related videos on Youtube

boom
Author by

boom

XML, C, C++, Cocoa, Gtk, Gtkmm, Gnome, zlib, libxml, Berkelium, OpenCV

Updated on July 08, 2022

Comments

  • boom
    boom almost 2 years

    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;
    
  • Tony Delroy
    Tony Delroy almost 14 years
    +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...?
  • Andy Dent
    Andy Dent over 12 years
    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.
  • user1135469
    user1135469 about 11 years
    Just wondering if anyone has come across this, or is it just me...I run cout<<"pos: "<<str.find("not in the string")<<" npos: "<<std::string::npos; and get pos:4294967295 npos: 4294967295 when I run it in Windows but on Mac I get pos:4294967295 npos: 18446744073709551615. That doesn't seem right...well any way I suggest comparing to -1 instead of std::string::npos
  • Dzyann
    Dzyann over 10 years
    @user1135469 if you see the answer of codaddict bellow (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.
  • NoSenseEtAl
    NoSenseEtAl almost 10 years
    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
  • sudheerbb
    sudheerbb over 4 years
    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.
  • Alex Guteniev
    Alex Guteniev about 4 years
    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.
  • einpoklum
    einpoklum about 2 years
    @anicicn: Yes, sorry, edited. You can also make such edits directly - SO is a community site :-)
  • anicicn
    anicicn about 2 years
    No problem, didn't want to edit your post :) Thanks for resolving.