What does string :: npos mean?

What does the following expression mean?

    string s="Joe Alan Smith"";
    cout << (s.find("Allen") == string::npos) << endl; 
+3
source share
3 answers

Actually string::find()returns the position of the found string, but if it does not find the given string, it returns string::nposwhere it nposmeans without a position .

npos- This is an unsigned value, the standard defines it as a -1(signed presentation), which does not indicate a position.

//npos is unsigned, that is why cast is needed to make it signed!
cout << (signed int) string::npos <<endl; 

Conclusion:

1

Take a look at Ideone: http://www.ideone.com/VRHUj

+14
source

http://www.cplusplus.com/reference/string/string/npos/

The return value is usually used to indicate a failure.

, , "Allen" s.

+1

The method .find()returns string::nposif it did not find the target string in the search string. This is an integer whose value cannot represent the value of the “found” index, usually -1. The valid index value is an integer> = 0, which is the index at which the target string was found.

+1
source

Source: https://habr.com/ru/post/1794237/


All Articles