Use of the Find () function and the RFind () function in C + +

Source: Internet
Author: User

This article is reproduced from http://blog.csdn.net/youxin2012/article/details/9162415

Application of Find () in string (RFind ()similar, just looking from reverse)The prototypes are as follows: (1) size_t find (const string& str, size_t pos = 0) const; Find Object--string Class object (2) size_t find (const char* s, size_t pos = 0) const;  Find Object--string (3) size_t find (const char* s, size_t pos, size_t N) const;  Find Object--the first n characters of a string (4) size_t find (char c, size_t pos = 0) const; Find Object--character result: Found--Returns the index of the first character not found--returns the String::npos example:
1#include <iostream>//Std::cout2#include <string>//std::string3   4 intMain ()5 {  6STD::stringSTR ("There is needles in this haystack with needles."); 7STD::stringSTR2 ("Needle"); 8   9   //different member versions of Find in the same order as above:Tenstd::size_t found =Str.find (STR2);  One   if(FOUND!=STD::string:: NPOs) AStd::cout <<"First ' needle ' found at:"<< found <<'\ n';  -    -Found=str.find ("Needles is small", found+1,6);  the   if(FOUND!=STD::string:: NPOs) -Std::cout <<"second ' needle ' found at:"<< found <<'\ n';  -    -Found=str.find ("Haystack");  +   if(FOUND!=STD::string:: NPOs) -Std::cout <<"' haystack ' also found at:"<< found <<'\ n';  +    AFound=str.find ('.');  at   if(FOUND!=STD::string:: NPOs) -Std::cout <<"Period found at:"<< found <<'\ n';  -    -   //Let ' s replace the first needle: -Str.replace (Str.find (STR2), Str2.length (),"preposition");//Replace usage -Std::cout << str <<'\ n';  in    -   return 0;  to}
Result: First ' needle ' found at:14second ' needle ' found at:44 ' haystack ' also found At:30period found At:51there is both pre Positions in this haystack with needles other also find_first_of (), find_last_of (), find_first_not_of (), find_last_not_of () function is In the Find string Find criteria that any character satisfiesString Snake1 ("Cobra"); int where = Snake1.find_first_of ("Hark"); returns 3 because the first occurrence of a character in "Hark" in Snake1--cobra is the character ' R ' (3 is C Obra the index of ' r ' in the same vein: int where = snake1.find_last_of ("Hark"); returns 4 because the last occurrence of one character in "Hark" in Snake1--cobra is the character ' a ' (3 for Cobra) Index of R ') 其他同理

Use of the Find () function and the RFind () function in C + +

Related Article

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.