string::npos in C++ with Examples
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 no matches.
static const size_t npos = -1;
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:
first for found at: 5
Explanation: In the above program string:npos constant is defined with a value of -1, because size_t is an unsigned integral type, and -1 is the largest possible representable value for this type.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.