C++

C++ string::npos

C++ string::npos static member is a measurable value equivalent to the largest value that the size_t kind may describe. The literal meaning varies depending on the situation, although it usually means “until the limit of the text” when included in the integer for a length argument. It is commonly used to signify that there are no matches. Since size_t is an unsigned numeric type, this permanent is declared with -1, the most feasible computable result. Let’s start the file creation in the Ubuntu terminal and launch it in the editor.

Example 01:

Let’s get started with this article’s first example of using string::npos in the code. As we know, it is a multipurpose constant static member; we can make it utilize the way we want. So we will be using it to find a specific string from another string. Therefore, we have started this code with C++ “bits/stdc++.h>” header library. After this library, we have to add the “std” standard namespace with the “using” keyword. All the working will be performed within the non-other than main() method of c++. We will not be utilizing any user-defined function in our code. Within our C++ code’s main() method, two string-type variables, v1 and v2, have been initialized with a string value in both. The second string variable, v2 is, holding a value to be searched in the variable v2.

Here comes the find() method to find the particular string using variable v2 in its arguments. The string variable v1 to be searched from will be attached to the function “find()” by a dot. This function will return the position of a string to be found from the other string and saved to the new integer type variable “n”. Here comes the if-else statement to see if the value has been found in the other string v1 or not using the string::npos. As string::npos holds “-1” which is negative and cannot be a location, it will be compared with the “n” variable value returned by the find function. If the position returned in the “n” variable doesn’t match with the strng::npos, it means the value has been found in the “n” position, and the cout statement will display that value.

On the other hand, if the value has not been found through the find() function, the value returned in variable “n” and string::npos will become equal. Therefore, the other part cout statement will execute to show that the string has not been found in another variable. Let’s save this code and run it.

After saving the code with Ctrl+S, we have compiled it with the C++ “g++” Ubuntu system compiler. After that, it has been executed and resulted in the below output showing that the string v1 is found at 1 index of the variable v2.

Example 02:

Let’s have another example to see the different functionality of “string::npos” this time. The code begins with three different header libraries of C++. These libraries include iostream, string, and bitset for utilizing the input-output stream, strings, and bit type variables in the code. The standard “Std” namespace is compulsory to use the standard statement in C++ code. Within the built-in main() function, we have initiated a string type variable “v1” with the string value. The cout statement is here to present the string v1 on the shell.

Here comes the initialization of another string variable, v1, by using the first string variable v1. It will be a substring of the first variable, v1. We have been using the indexes of characters in variable v1 from 0 to 3 to make a new substring. The cout statement will display the substring just created. This is a simple technique to create a substring in C++. But, we can also use the string::npos to create a substring from a specific index of the first variable to its end. The same way has been used to create a substring v3 from variable v1. The starting index has been defined as 4, and the last index will not be defined, but we will use the string::npos variable to make it up to the end of the file. Now a new substring will be displayed using the cout line.

After the code compilation, no errors have been found so far. The code execution returns the three-line output as shown below. On the first line, the original string is displayed. On the third line, a substring made by indexes has been displayed. On the last, the substring made using start index and string::npos is displayed.

Example 03:

Let’s come up with another use of string::npos constant member. We will be using it to convert some characters to bits. Let’s start this code with three headers, i.e. iostream, string, and bitset. Same iostream, bitset, and string header libraries have been added. The namespace “Std” is here also. Within the main() function, We have been declaring a bitset named “bs” of size 5. This bitset has been using the 5 characters “xxyyx” string as converted into a string. Then “String::npos” member has been used or applied on both “x” and “y” characters separately. As we have been using the “x” character first, it will be converted to 0. Also, we are using “y” at the last position; this is why it will convert it to bit 1. The cout statement is here to display the value of bitset “bs” that has just been converted via the string::npos on a string of size 5.

This code must be compiled with g++ first. After running compiled code, we have got the result of bitset in bits, i.e. 00110.

Example 04:

Here comes the bonus example to use string::npos to compare the find() method. The single iostream library is utilized before the standard “Std” namespace of C++. The main () method has been initializing 2 variables of string types, i.e. s1 and s2. The find() function looks for a string s2 in a string variable s1 and saves the returned index within the integer variable “n”. This returned index in the “n” variable has been compared with the “string::npos” member containing -1 as its value. If the index returned in the “n” variable doesn’t match with “string::npos” having a -1 value, it will show that the string has been found in the shell using the cout statement. Else, another cout statement will be executed showing that the string s1 has not been found in the variable s2. Let’s save our code with Ctrl+S and quit with Ctrl+X.

The file npos.cc is compiled using the “g++” compiler of Ubuntu 20.04. The execution shows that the substring is found at the 5th index of variable s1, and “n” is not equal to “string::npos”.

Conclusion:

Finally, we have well-explained the string::npos data member od type size_t in C++ code examples. The examples cover comparing two strings while utilizing the “string::npos” as -1. It is also used to create a substring up to the end of string length. Additionally, the bitset has also been produced. Therefore, we can say that this article will be best for C++ users.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.