C++ string类中的字符串查找

C++ string类中的字符串查找


    类string提供了大量查找功能和搜索功能,其中比较经常使用的查找和搜索函数是find()函数、
find_first_not_of()函数、find_first_of()函数、find_last_not_of()函数、find_last_of()函数、rfind()等。
    
    find()函数的语法以下所示:
    (1) size_type find(E c, size_type pos = npos) const;用来查找单个字符在字符串中出现的位置并返回
        该位置基于0的索引值。
    (2) size_type find(const E *s, size_type pos = npos) const;用来查找以空字符结尾的字符数组在
        字符串中出现的位置并返回该位置基于0索引值。
    (3) size_type find(const E *s, size_type pos, size_type n = npos) const;用来查找以空字符结尾的
        字符数组在字符串中出现的位置并返回该位置基于0索引值,它是从npos开始查找的。
    (4) size_type find(const basic_string &str, size_type pos = npos) const;用来查找字符串而且返回
        该搜索的字符串的基于0索引值的位置值,它是从npos开始查找的。


    find()函数的功能是从std::string对象的头部顺序找目标值,若是找到返回该目标值出现的位置,若是没有在
字符串对象中找到目标对象,返回值为-1。


    rfind()函数的语法以下所示:
    (1) size_type rfind(E c, size_type pos = npos) const;用来反向查找单个字符在字符串中出现的位置
        并返回该位置基于0的索引值。
    (2) size_type rfind(const E *s, size_type pos = npos) const;用来反向查找以空字符结尾的字符数组
        在字符串中出现的位置并返回该位置基于0索引值。
    (3) size_type rfind(const E *s, size_type pos, size_type n = npos) const;用来反向查找以空字符
        结尾的字符数组在字符串中出现的位置并返回该位置基于0索引值,它是从npos开始查找的。
    (4) size_type rfind(const basic_string &str, size_type pos = npos) const;用来反向查找字符串而且
        返回出现该搜索的字符串的基于0索引值的位置值,它是从npos开始查找的。
    rfind()函数的功能是从std::sring对象的尾部反向查找目标值,若是找到返回该目标值出现的位置,若是没有
在字符串对象中找到目标对象,返回值为-1。


    find_first_not_of()函数的常见语法以下所示:
    size_type find_first_not_of(E c, size_type pos = 0) const;
    size_type find_first_not_of(const E *s, size_type pos = 0) const;
    size_type find_first_not_of(const E *s, size_type pos, size_type n) const;
    size_type find_first_not_of(const basic_string &str, size_type pos = 0) const;
    该函数的功能是在string对象中查找对象,若是在string出现了彻底不匹配的字符,字符串或以空字符结尾的
字符数组时,系统显示第一次出现这种情形的位置。若是定义了pos,从pos开始搜索。


    find_first_of()函数的常见语法以下所示:
    size_t find_first_of( const string& str, size_t pos = 0 ) const;
    size_t find_first_of( const char* s, size_t pos, size_t n ) const;
    size_t find_first_of( const char* s, size_t pos = 0 ) const;
    size_t find_first_of( char c, size_t pos = 0 ) const;
    该函数的功能是在string对象中查找对象,若是在string出现了任意彻底匹配的字符,字符串或以空字符结尾
的字符数组时,系统显示第一次出现这种情形的位置。若是定义了pos,从pos开始搜索。只要在string对象中出现了
匹配对象,当即返回位置。


    find_last_not_of()函数的常见语法以下所示:
    size_t find_last_not_of( const string& str, size_t pos = npos ) const;
    size_t find_last_not_of( const char* s, size_t pos, size_t n ) const;
    size_t find_last_not_of( const char* s, size_t pos = npos ) const;
    size_t find_last_not_of( char c, size_t pos = npos ) const;
    该函数的功能是在string对象中反向查找对象,若是在string出现了任意彻底不匹配的字符,字符串或以空
字符结尾的字符数组时,系统显示第一次彻底不匹配时出现的位置。若是定义了pos,从pos反向开始搜索。只要
在string对象中出现了彻底不匹配的对象,当即返回位置值。


    find_last_of()函数的常见语法以下所示:
    size_t find_last_of( const string& str, size_t pos = npos ) const;
    size_t find_last_of( const char* s, size_t pos, size_t n ) const;
    size_t find_last_of( const char* s, size_t pos = npos ) const;
    size_t find_last_of( char c, size_t pos = npos ) const;
    该函数的共是在string对象中反向查找对象,若是在string出现了任意彻底匹配的字符,字符串或以空字符结
尾的字符数组时,系统显示第一次出现这种情形的位置。若是定义了pos,从pos开始反向搜索。只要在string对象
中出现了匹配的对象,则当即返回位置值。

ios

    字符串查找实例find_first_of()web

[cpp] view plaincopy数组

<EMBED id=ZeroClipboardMovie_1 height=18 name=ZeroClipboardMovie_1 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=1&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">app

  1. #include <iostream>  函数

  2. #include <string>  oop

  3.   

  4. using namespace std;  spa

  5.   

  6. int main(int argc, char *argv[])  .net

  7. {  orm

  8.     string str1("Heartbeat");  对象

  9.     string str2("abcde");  

  10.     int iPos = 0;  

  11.   

  12.   

  13.     cout << "The string to search is '" << str1.c_str() << "'" << endl;  

  14.     //find the first instance in str1 of any characters in str2  

  15.     iPos = str1.find_first_of(str2, 0);  

  16.     cout << "Element in '" << str2.c_str() << "' found at position " << iPos << endl;  

  17.   

  18.     //start looking in the third position  

  19.     iPos = str1.find_first_of(str2, 2);  

  20.     cout << "Element in '" << str2.c_str() << "' found at position " << iPos << endl;  

  21.   

  22.     //use an array of the element type as the set of elements to search for;  

  23.     //look for anything after the fourth position  

  24.     char achVowels[] = {'a''e''i''o''u'};  

  25.     iPos = str1.find_first_of(achVowels, 4, sizeof(achVowels));  

  26.     cout << "Element in '";  

  27.     for (int i = 0; i < sizeof(achVowels); ++i)  

  28.     {  

  29.         cout << achVowels[i];  

  30.     }  

  31.     cout << "' found at position " << iPos << endl;  

  32.   

  33.     //use a string literal to specify the set of elements  

  34.     char szVowels[] = "aeiou";  

  35.     iPos = str1.find_first_of(szVowels, 0);  

  36.     cout << "Element in '" << szVowels << "' found at position " << iPos << endl;  

  37.   

  38.     //look for a specific character beginning in the third position  

  39.     iPos = str1.find_first_of('e', 2);  

  40.     cout << "'e' found at position " << iPos << endl;  

  41.   

  42.     return 0;  

  43. }  

运行结果为:

The string to search is 'Heartbeat'Element in 'abcde' found at position 1Element in 'abcde' found at position 2Element in 'aeiou' found at position 6Element in 'aeiou' found at position 1'e' found at position 6

相关文章
相关标签/搜索