# Include <string>
# Include <iostream>
Using namespace std;
Void main ()
{
Copy codeThe Code is as follows:
//// Size_type returned by the find Function
String s ("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i ");
String flag;
String: size_type position;
// The find function returns the subscript position of jk in s.
Position = s. find ("jk ");
If (position! = S. npos) // if not found, a special sign c ++ is returned, which is represented by npos. Here, the npos value is 4294967295,
{
Cout <"position is:" <position <endl;
}
Else
{
Cout <"Not found the flag" + flag;
}
Copy codeThe Code is as follows:
// The find function returns the subscript position of any character in the flag for the first time in s.
Flag = "c ";
Position = s. find_first_of (flag );
Cout <"s. find_first_of (flag) is:" <position <endl;
Copy codeThe Code is as follows:
// Search for string B starting from string s subscript 5, and return the subscript of string B in s
Position = s. find ("B", 5 );
Cout <"s. find (B, 5) is:" <position <endl;
Copy codeThe Code is as follows:
// Find all the positions where the flag appears in s.
Flag = "";
Position = 0;
Int I = 1;
While (position = s. find_first_of (flag, position ))! = String: npos)
{
// Position = s. find_first_of (flag, position );
Cout <"position" <I <":" <position <endl;
Position ++;
I ++;
}
Copy codeThe Code is as follows:
// Find the first position in the flag that does not match s
Flag = "acb12389efgxyz789 ";
Position = flag. find_first_not_of (s );
Cout <"flag. find_first_not_of (s):" <position <endl;
Copy codeThe Code is as follows:
// Reverse lookup, the last position of the flag in s
Flag = "3 ";
Position = s. rfind (flag );
Cout <"s. rfind (flag):" <position <endl;
}
Note:
1. If string sub = "abc";
String s = "cdeabcigld";
S. find (sub), s. rfind (sub): If the two functions are completely matched, the matching index is returned. That is, the current index is returned only when s contains three consecutive letters (abc.
S. find_first_of (sub), s. find_first_not_of (sub), s. find_last_of (sub), s. the four functions find_last_not_of (sub) are used to search for indexes in s that contain any letter in sub.
2. If no query is found, string: npos is returned, which is a large number and its value does not need to be known.