![]() |
VOOZH | about |
Syntax:
static const size_t npos = -1;
Where npos is a constant static value with the highest possible value for an element of type size_t and it is defined with -1.
Note: std::string::npos is a constant that holds the largest possible value of size_t type ( 18446744073709551615 on 64-bit systems ), which is an unsigned integer type. Hence, -1 corresponds to the actual value of std::string::npos.
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.
What if the valid position for a substring is not found in a string?
Various member functions of the String class return the default value of std::string::npos if a valid position or index for a substring is not found in the string.
Below are the String Functions that return the value of std::string::npos in the case of failure:
Program 2: C++ Program to Illustrate that Some String Functions return the value of std::string::npos in case of failure.
Substring not found