Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does string::npos mean in this code?

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; 
like image 595
boom Avatar asked Sep 30 '10 05:09

boom


People also ask

What value is string :: NPOS?

What is string::npos in C++? npos is a constant static member value with the greatest possible value for an element of type size_t. This value, when used as the value for a len parameter in string's member functions, means until the end of the string. This constant is defined with a value of -1.

What is string :: in C++?

C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.

What type is std::string?

The std::string type is the main string datatype in standard C++ since 1998, but it was not always part of C++. From C, C++ inherited the convention of using null-terminated strings that are handled by a pointer to their first element, and a library of functions that manipulate such strings.

What is the difference between string and std::string?

Differences between std::string and String Pavan. std::string is the string class from the standard C++ library. String is some other string class from some other library. It's hard to say from which library, because there are many different libraries that have their own class called String.

What does NPOs mean in string?

What is string::npos: It is a constant static member value with the highest possible value for an element of type size_t. It actually means until the end of the string. It is used as the value for a length parameter in the string ’s member functions. As a return value, it is usually used to indicate ...

What is the NPOs value in C++?

Where, npos is constant static value with the highest possible value for an element of type size_t and it is defined with -1. Program 1: Below is the C++ program to illustrate the use of string::npos:

How to avoid error-1 when comparing idX and string NPOs?

idx == std::string::npos might yield false if idx has the value -1and idx and string::nposhave 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:

Should I use codaddict-1 or NPOs?

@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.


1 Answers

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.

like image 189
Brian R. Bondy Avatar answered Sep 30 '22 23:09

Brian R. Bondy