What does the string :: npos mean

What does the expression string::npos here

 found=str.find(str2); if (found!=string::npos) cout << "first 'needle' found at: " << int(found) << endl; 
+75
c ++
Sep 30 '10 at 5:12
source share
10 answers

This means that it is not found.

It is usually defined as follows:

 static const size_t npos = -1; 

Better to compare with npos instead of -1, because the code is more legible.

+85
Sep 30 '10 at 5:15
source share

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

+47
Sep 30 '10 at 5:18
source share

The string::npos says:

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

The return value is usually used to indicate a failure.

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

+22
Sep 30 '10 at 5:26
source share

size_t is an unsigned variable, so "unsigned value = - 1" automatically makes it as large as possible for size_t : 18446744073709551615

+15
Oct 20 '13 at
source share

std::string::npos is a specific implementation index that always goes beyond any instance of std::string . Various std::string functions return it or accept it to signal the end of a string situation. Usually this is some kind of unsigned integer, and its value is usually std::numeric_limits<std::string::size_type>::max () , which (thanks to standard integer promotions) is usually comparable to -1 .

+8
Sep 30 '10 at 5:21
source share

we must use string::size_type for the return type of the find function, otherwise comparing with string::npos may not work. size_type , which is determined by the row dispenser, must be an unsigned integral type. The default dispenser, dispenser, uses the size_t type as size_type . Because -1 converted to 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 ​​are different. In fact, (unsigned long)-1 is different from (unsigned short)- 1 if the size types are different. Thus comparing

 idx == std::string::npos 

can give false if idx is -1 , and idx and string::npos are 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 if the search worked directly:

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

However, often you need an index of the corresponding character position. So another simple solution is to define your own sign value for npos:

 const int NPOS = -1; 

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

 if (idx == NPOS) { // works almost always ... } 
+3
Mar 18 '15 at 7:43
source share

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

+2
Sep 30 '10 at 5:15
source share
 $21.4 - "static const size_type npos = -1;" 

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

+1
Sep 30 '10 at 5:27
source share

npos is just a token that tells you that find () did not find anything (maybe -1 or something like that). find () checks the first occurrence of a parameter and returns the index at which the parameter begins. For example,

  string name = "asad.txt"; int i = name.find(".txt"); //i holds the value 4 now, that the index at which ".txt" starts if (i==string::npos) //if ".txt" was NOT found - in this case it was, so this condition is false name.append(".txt"); 
0
Dec 14 '16 at 5:44
source share

static constant size_t npos = -1;

The maximum value for size_t

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

This value, when used as the value of the len (or sublen) parameter in string member functions, means "to the end of the string."

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

This constant is determined by the value -1, which, since size_t is an unsigned integer type, is the maximum possible representable value for this type.

0
Jul 30 '19 at 4:53 on
source share



All Articles