What is npos ??? I recently saw it in some source code and really want to know what it is used for and why it is used. An example would be nice also. Thanks(:
Asked
Active
Viewed 112 times
-8
-
2Does this answer your question? [What does string::npos mean in this code?](https://stackoverflow.com/questions/3827926/what-does-stringnpos-mean-in-this-code) – asmmo May 25 '20 at 04:53
-
See https://en.cppreference.com/w/cpp/string/basic_string/npos. – eesiraed May 25 '20 at 04:55
-
2@chiefus If you wrote that "I recently saw it in some source code" then you already have an example. – Vlad from Moscow May 25 '20 at 04:58
-
Ya @Moscow but the example is too complicated for me just want a more basic one. – chiefus May 25 '20 at 05:04
-
You could look for documentation on the function that was being used in that code. For example, look at the return value of [`std::string::find`](https://en.cppreference.com/w/cpp/string/basic_string/find). – Blastfurnace May 25 '20 at 05:10
1 Answers
3
The documentation says :
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.
It is defined internally as :
static const size_t npos = -1;
Just remember, it's used to indicate not found.

Abhishek Bhagate
- 5,583
- 3
- 15
- 32