Analysis of Find series function used in c++string

Source: Internet
Author: User

General statement:
All of the string lookup functions described below have a unique return type, which is Size_type, an unsigned integer (calculated as printed). If the lookup succeeds, returns the position of the first character or substring found by the find rule, and if the lookup fails, returns NPOs, which is 1 (printed out as 4294967295).

1.fine ()

Prototype:

String (1) size_type find (const basic_string& str, Size_type pos = 0) const noexcept;//c-string (2) Size_type Find (c Onst chart* s, size_type pos = 0) Const;//buffer (3) Size_type find (const chart* s, size_type pos, size_type N) const;//ch Aracter (4) Size_type find (CharT c, size_type pos = 0) const noexcept;

Example:

#include <iostream> #include <string>using namespace Std;int main () {//Test size_type find (CharT C, Size_type PO    s = 0) const noexcept;    String St1 ("Babbabab"); cout << st1.find (' a ') << ENDL;//1 is known by the prototype, if the 2nd argument is omitted, the default starting from position 0 (that is, 1th character) to find cout << st1.find (' A ', 0) <& Lt 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), find the character A, return the first match position, and if the match fails, return NPOs cout << st1.rfind (' A ', 7) << ENDL;//6 about RFind, followed by cout << st1.find (' C ', 0) << endl;//4294967295 cout << (St1.find (' C ', 0) = = 1) << endl;//1 cout & lt;< (St1.find (' C ', 0) = = 4294967295) << endl;//1 both output 1 because the computer-1 and 4294967295 are represented as 32 1 (binary) cout << st1 . Find (' a ', +) << endl;//4294967295 when the starting position of the lookup exceeds the string length, press find failure handling, return NPOs//test Size_type find (const BASIC_STRING&AMP ;    str, Size_type pos = 0) const noexcept;    String St2 ("AABCBCABCBABCC"); String str1("abc"); cout << St2.find (str1, 2) << ENDL;//6 from position 2 (b) of St2, returns the first character of the matched string (ABC) in ST2 position, failed to return NPOs//test size_t    Ype Find (const chart* s, size_type pos = 0) const; cout << st2.find ("abc", 2) << Endl;    6 Ibid, except that the parameter is not a string but char*//test Size_type Find (const chart* s, size_type pos, size_type N) const;  cout << st2.find ("ABCDEFG", 2, 3) << ENDL;//6 takes ABCDEFG to get the first 3 characters (ABC) to participate in the match, equivalent to St2.find ("abc", 2) cout << St2.find ("ABCBC", 0, 5) << ENDL;//1 equivalent to St2.find ("ABCBC", 0) cout << st2.find ("ABCBC", 0, 6) << end l;//4294967295 the 3rd parameter exceeds the length of the 1th parameter, returns NPOs return 0;}

Application Examples:

Find All "abc" (output position) in string str, output "not find!" if not found #include <iostream> #include <string>using namespace Std;int main () {    string str (" BABCCBABCAABCCCBABCCABCABCABBABCC ");    int num = 0;    size_t fi = Str.find ("abc", 0);        while (Fi!=str.npos)    {        cout << fi << "   ";        num++;        fi = Str.find ("abc", FI + 1);    }    if (0 = = num)        cout << "not find!";    cout << Endl;    return 0;} Operation Result://1   6   29

2.rfind ()

Prototype:

String (1) size_type rfind (const basic_string& str, Size_type pos = NPOs) const noexcept;//c-string (2) Size_type RFI nd (const chart* s, size_type pos = NPOs) Const;//buffer (3) size_type RFind (const chart* S, size_type pos, size_type N) c Onst;//character (4) Size_type rfind (CharT c, size_type pos = npos) const noexcept;

Description
RFind () is similar to find (), except that the lookup order is different, and rfind () is looking forward from the specified position to the top of the string. For example, in the above example, St1.rfind (' a ', 7) is a sentence from the ST1 position 7 (the last character of St1 B) to find the character a, the first time to find the 2nd character A, so return 6.
About RFind (), no longer detailed, readers can write code to learn RFind () based on the example of Find ().

3.find_first_of ()

Prototype:

String (1) size_type find_first_of (const basic_string& str, Size_type pos = 0) const noexcept;//c-string (2) Size_typ e find_first_of (const chart* s, size_type pos = 0) Const;//buffer (3) size_type find_first_of (const chart* s, Size_type p OS, Size_type N) const;//character (4) Size_type find_first_of (CharT c, size_type pos = 0) const noexcept;

Description
Looking backwards from position pos in the source string, whenever a character is encountered in the source string, the character is the same as any character in the target string, stops the lookup, returns the position of the character in the source string, and returns NPOs if the match fails.

Example (give only examples of partial prototypes, for the rest of the prototypes, it is believed that the reader has the ability to follow the example of Find () from the line to write code testing and learning):

#include <iostream> #include <string>using namespace Std;int main () {    //test 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) with Str.find (' a ', 0)    //test 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;//starting with the No. 0 character B of str1, B does not match any character in str2; find C,c does not match any character in str2; find G again,                                                //g matches the G in str2, so stop looking, return G position in STR1 2    //test size_type find_first_of (const chart* S, size_type pos, Size_type N) const;    cout << str1.find_first_of ("Kghlj", 0);//2 Although the   3rd parameter exceeds the length of the KGHLJ, but still can get the correct result, it can be considered that str1 is and "kghlj+ garbled" do match    return 0;}

Application Examples:

Replace all vowels in the string with *//code from C + + Reference, Address: http://www.cplusplus.com/reference/string/basic_string/find_first_of/# Include<iostream> #include <string>using namespace Std;int main () {    std::string str ("Please, replace The vowels sentence by asterisks. ");    Std::string::size_type found = str.find_first_of ("Aeiou");    while (found! = Std::string::npos)    {        Str[found] = ' * ';        Found = str.find_first_of ("Aeiou", found + 1);    }    Std::cout << str << ' \ n ';    return 0;} Operating result://pl**s* r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks


4.find_last_of ()

Prototype:

String (1) size_type find_last_of (const basic_string& str, Size_type pos = NPOs) const noexcept;//c-string (2) size_t Ype find_last_of (const chart* s, size_type pos = NPOs) Const;//buffer (3) size_type find_last_of (const chart* S, Size_typ e Pos, Size_type N) const;//character (4) Size_type find_last_of (CharT c, size_type pos = npos) const noexcept;

Description
This function is similar to the find_first_of () function, except that the lookup order is forward from the specified position, here is a simple example, no longer repeat, the reader can refer to find_first_of () to learn by themselves.

Example:

#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 two C in the source string, and the remaining characters do not match    string str ("ABCDECG");    cout << str.find_last_of ("HJLYWKCIPN", 6) << ENDL;//5   from str position 6 (g) to start looking for, G mismatch, then find C,c match, stop looking for, Returns the position of C in Str 5    cout << str.find_last_of ("HJLYWKCIPN", 4) << ENDL;//2   from the location of str 4 (e) to start looking for, E mismatch, then find D, D do not match, then find C,c match, stop find,                                                      //    return C position in str 5    cout << str.find_last_of ("HJLYWKCIPN", "$" << ENDL;//5   when the 2nd parameter exceeds the length of the source string (where Str is 7), there is no error, which is equivalent to starting from the last//character of the source string to    find    return 0;}

5.find_first_not_of ()
Prototype:

String (1) size_type find_first_not_of (const basic_string& str, Size_type pos = 0) const noexcept;//c-string (2) size  _type find_first_not_of (const chart* s, size_type pos = 0) Const;//buffer (3) size_type find_first_not_of (const charT* S, Size_type Pos, Size_type N) const;//character (4) Size_type find_first_not_of (CharT c, size_type pos = 0) const noexcept;

Description
Looking backward from position pos in the source string, as long as the source string encounters a character that is not the same as any character in the target string, stops the lookup, returns the position of the character in the source string, and returns the NPOs if the entire source string has been traversed without finding a character that satisfies the condition.

Example (just for example, with the front of the study, I believe readers can learn find_first_not_of ()):

#include <iostream> #include <string>using namespace Std;int main () {    //test 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   from the source string str position 0 (a) to start looking for a (match) in the target string, and then find the B,b match, Then find C,c match,                                                              //    find D, no D (mismatch) in the target string, stop searching, return D in STR position 3    return 0;}

Replace all non-vowels in the string with * by replacing the fing_first_of () in the previous application example with Find_first_not_of (). Ask the reader to verify it yourself.

6.find_last_not_of ()

Prototype:

String (1) size_type find_last_not_of (const basic_string& str, Size_type pos = NPOs) const noexcept;//c-string (2) Si  Ze_type find_last_not_of (const chart* s, size_type pos = NPOs) Const;//buffer (3) size_type find_last_not_of (const charT* S, Size_type Pos, Size_type N) const;//character (4) Size_type find_last_not_of (CharT c, size_type pos = npos) const NOEX Cept

Description
Find_last_not_of () is similar to find_first_not_of () except that the search order is forward from the specified position and is no longer mentioned here, believing that the reader can learn by himself.

Analysis of Find series function used in c++string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.