An explanation of the find function in the string class

All the string lookup functions described below have a unique return type, which is size_type, which is an unsigned integer (as printed). If the search succeeds, return the position of the first character or substring found according to the search rule; if the search fails, return npos, which is -1 (printed out as 4294967295).

(1) string::find function

#include<iostream>
#include<string>
using namespace std;
int main()
{
    //测试size_type find (charT c, size_type pos = 0) const noexcept;
    string st1("babbabab");
    cout << st1.find( ' a ' ) << endl; // 1 is known from the prototype. If the second parameter is omitted, 
    cout << st1.find starts from position 0 (ie the first character) by default ( ' a ' , 0 ) << endl; // 1 
    cout << st1.find( ' a ' , 1 ) << endl; // 1    
    cout << st1.find( ' a ' , 2 ) << endl ; // 4 In st1, starting from position 2 (b, including position 2), look for character a, return the position of the first match, if the match fails, return npos 
    cout << st1.rfind( ' a ' ,7) << endl;//6 About rfind, 
    cout << st1.find( ' c ' , 0 ) << endl; // 4294967295 
    cout << (st1.find( ' c ' , 0 ) == - 1 ) << endl; / / 1 
    cout << (st1.find( ' c ' , 0 ) == 4294967295 ) << endl; // 1 Both sentences output 1, because -1 and 4294967295 are both represented as 32 1s (binary) in the computer 
    cout << st1.find( ' a ' , 100 ) << endl;// 4294967295 When the starting position of the search exceeds the length of the string, it is handled as the search failure, and npos is returned
     //测试size_type find (const basic_string& str, size_type pos = 0) const noexcept;
    string st2("aabcbcabcbabcc");
    string str1("abc");
    cout << st2.find(str1, 2 ) << endl; // 6 Start matching from position 2 (b) of st2, and return the position of the first character of the matching string (abc) in st2 when the first successful match is made , return npos on failure
     // test size_type find (const charT* s, size_type pos = 0) const; 
    cout << st2.find( " abc " , 2 ) << endl; // 6 Same as above, except that the parameter is not a string is char*
     // test size_type find (const charT* s, size_type pos, size_type n) const; 
    cout << st2.find( " abcdefg " , 2 , 3 ) << endl; // 6 take the first 3 of abcdefg The character (abc) participates in the matching, which is equivalent to st2.find("abc", 2) 
    cout << st2.find("abcbc " , 0 , 5 ) << endl; // 1 is equivalent to st2.find("abcbc", 0) 
    cout << st2.find( " abcbc " , 0 , 6 ) << endl; // 4294967295 3rd When the first parameter exceeds the length of the first parameter, return npos 
    return  0 ;
}

(2) string::rfind() function

 rfind() is very similar to find(), the difference is that the search order is different. rfind() searches forward from the specified position until the beginning of the string. For example, the st1.rfind('a',7) sentence in the above example is to search for the character a from the position 7 of st1 (the last character b of st1), and the second to last character a is found for the first time. So return 6.

(3) string::find_first_of() function

Search backwards from position pos in the source string, as long as a character is encountered in the source string, and the character is the same as any character in the target string , the search stops, and the position of the character in the source string is returned; if the match fails , returns npos.

#include<iostream>
#include<string>
using namespace std;
int main()
{
    //测试size_type find_first_of (charT c, size_type pos = 0) const noexcept;
    string str("babccbabcc");
    cout << str.find('a', 0) << endl;//1
    cout << str.find_first_of('a', 0) << endl;//1   str.find_first_of('a', 0)与str.find('a', 0)
    //测试size_type find_first_of (const basic_string& str, size_type pos = 0) const noexcept;
    string str1("bcgjhikl");
    string str2("kghlj");
    cout << str1.find_first_of(str2, 0 ) << endl; // Start from the 0th character b of str1, b does not match any character in str2; then find c, c does not match any character in str2 Match; find g again,
                                                 // g matches g in str2, so stop searching, return g's position 2 in str1
     // test size_type find_first_of (const charT* s, size_type pos, size_type n) const; 
    cout << str1.find_first_of( " kghlj " , 0 , 20 ); // 2 Although the third parameter exceeds the length of kghlj, the correct result can still be obtained. It can be considered that str1 is matched with "kghlj+garbled" 
    return  0 ;
}

(4) string::find_last_of() function

This function is similar to the find_first_of() function, except that the search order is from the specified position forward

#include<iostream>
#include<string>
using namespace std;
int main()
{
    // Test size_type find_last_of (const charT* s, size_type pos = npos) const;
     // Only the character c in the target string matches the two c in the source string, and the rest of the characters do not match 
    string str( " abcdecg " );
    cout << str.find_last_of( " hjlywkcipn " , 6 ) << endl; // 5 Start from the position 6(g) of str and want to search forward, g does not match, then find c, c match, stop the search, return c in Position 5 in str 
    cout << str.find_last_of( " hjlywkcipn " , 4 ) << endl; // 2 Start from position 4(e) of str and want to search forward, e does not match, then find d, d does not match, Find c again , match c, stop searching,
                                                       //     return the position of c in str 5 
    cout << str.find_last_of( " hjlywkcipn " , 200 ) << endl;// 5 When the second parameter exceeds the length of the source string (here the str length is 7), there will be no error, which is equivalent to starting from the last of the source string
                                                         //    start looking for characters 
    return  0 ;
}

(5)string::find_first_not_of()函数

Search backwards from position pos in the source string, as long as a character is encountered in the source string, and the character is different from any character in the target string, the search is stopped and the position of the character in the source string is returned; if After traversing the entire source string, if no characters that meet the conditions are found, npos is returned.

#include<iostream>
#include<string>
using namespace std;
int main()
{
    //测试size_type find_first_not_of (const charT* s, size_type pos = 0) const;
    string str("abcdefg");
    cout << str.find_first_not_of( " kiajbvehfgmlc " , 0 ) << endl; // 3 Start searching from position 0(a) of the source string str, there is a (match) in the target string, then find b, b to match, and then Find c, c match,
                                                               //     find d again, there is no d in the target string (no match), stop the search, return the position 3 of d in str 
    return  0 ;
}

(6)string::find_last_not_of()函数

find_last_not_of() is similar to find_first_not_of(), except that the search order is from the specified position forward

Reprinted from: https://www.cnblogs.com/zpcdbky/p/4471454.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325127097&siteId=291194637