C++ string 类的 find 方法实例详解

1、C++ 中 string 类的 find 方法列表
C++ string 类的 find 方法实例详解C++ string 类的 find 方法实例详解

size_type std::basic_string::find(const basic_string &__str, size_type __pos);
size_type std::basic_string::find(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::find(const _CharT *__s, size_type __pos);
size_type std::basic_string::find(_CharT __c, size_type __pos);

size_type std::basic_string::rfind(const basic_string &__str, size_type __pos);
size_type std::basic_string::rfind(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::rfind(const _CharT *__s, size_type __pos);
size_type std::basic_string::rfind(_CharT __c, size_type __pos);

size_type std::basic_string::find_first_of(const basic_string &__str, size_type __pos);
size_type std::basic_string::find_first_of(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::find_first_of(const _CharT *__s, size_type __pos);
size_type std::basic_string::find_first_of(_CharT __c, size_type __pos);

size_type std::basic_string::find_last_of(const basic_string &__str, size_type __pos);
size_type std::basic_string::find_last_of(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::find_last_of(const _CharT *__s, size_type __pos);
size_type std::basic_string::find_last_of(_CharT __c, size_type __pos);

size_type std::basic_string::find_first_not_of(const basic_string &__str, size_type __pos);
size_type std::basic_string::find_first_not_of(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::find_first_not_of(const _CharT *__s, size_type __pos);
size_type std::basic_string::find_first_not_of(_CharT __c, size_type __pos);

size_type std::basic_string::find_last_not_of(const basic_string &__str, size_type __pos);
size_type std::basic_string::find_last_not_of(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::find_last_not_of(const _CharT *__s, size_type __pos);
size_type std::basic_string::find_last_not_of(_CharT __c, size_type __pos);

View Code
本文以代码和运行实例进行解析,实例中的颜色部分的规则如下:

黄色底色:在原字符串中经过了搜索,没有黄色底色的表示不在我们的搜索范围内

红色字体:搜索错误的地方

绿色字体:搜索正确


下面逐个解析

find()、

rfind()、

find_first_of()、

find_last_of()、

find_first_not_of()、

find_last_not_of()


2、find()

find函数在C++的头文件basic_string.h中有四种定义,定义如下:

2.1)第一个定义:size_type find(const _CharT* __s, size_type __pos, size_type __n) const;
C++ string 类的 find 方法实例详解C++ string 类的 find 方法实例详解

/**
       *  @brief  Find position of a C substring.
       *  @param __s  C string to locate.
       *  @param __pos  Index of character to search from.
       *  @param __n  Number of characters from @a s to search for.
       *  @return  Index of start of first occurrence.
       *
       *  Starting from @a __pos, searches forward for the first @a
       *  __n characters in @a __s within this string.  If found,
       *  returns the index where it begins.  If not found, returns
       *  npos.
      */
      size_type
      find(const _CharT* __s, size_type __pos, size_type __n) const;

View Code
实例代码:

1 void xx_find_3()
 2 {
 3     printf("%s():n", __func__);
 4     const string strSrc = "abcdefghijklmnopqrstuvwxyz";
 5     const char * pStr = NULL;
 6     int pos = 0;
 7 
 8     pStr= "cdefppp";
 9     pos = strSrc.find(pStr, 0, 4); 
10     printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
11 
12     pStr= "cdefppp";
13     pos = strSrc.find(pStr, 0, 5); 
14     printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
15 
16     pStr= "cdefppp";
17     pos = strSrc.find(pStr, 5, 5); 
18     printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
19 }

运行结果:

xx_find_3():
pos =  2    [find]    abcdefghijklmnopqrstuvwxyz
pos = -1    [not find]  abcdefghijklmnopqrstuvwxyz
pos = -1    [not find]  abcdefghijklmnopqrstuvwxyz

2.2)第二个定义:size_type find(const basic_string& __str, size_type __pos = 0) const
C++ string 类的 find 方法实例详解C++ string 类的 find 方法实例详解

/**
       *  @brief  Find position of a string.
       *  @param __str  String to locate.
       *  @param __pos  Index of character to search from (default 0).
       *  @return  Index of start of first occurrence.
       *
       *  Starting from @a __pos, searches forward for value of @a __str within
       *  this string.  If found, returns the index where it begins.  If not
       *  found, returns npos.
      */
      size_type
      find(const basic_string& __str, size_type __pos = 0) const
    _GLIBCXX_NOEXCEPT
      { return this->find(__str.data(), __pos, __str.size()); }

View Code
实例代码:

void xx_find_1()
{
    printf("%s():n", __func__);
    const string strSrc = "abcdefghijklmnopqrstuvwxyz";
    string strDst;
    size_t pos = 0;

    strDst= "abcdef";
    pos = strSrc.find(strDst);
    printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");

    pos = strSrc.find(strDst, 0); 
    printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");

    strDst = "cdef";
    pos = strSrc.find(strDst, 2); 
    printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");

    pos = strSrc.find(strDst, 3); 
    printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
}

运行结果:

xx_find_1():
pos =  0    [find]    abcdefghijklmnopqrstuvwxyz
pos =  0    [find]    abcdefghijklmnopqrstuvwxyz
pos =  2    [find]    abcdefghijklmnopqrstuvwxyz
pos = -1    [not find]  abcdefghijklmnopqrstuvwxyz

2.3)第三个定义:size_type find(const _CharT* __s, size_type __pos = 0) const
C++ string 类的 find 方法实例详解C++ string 类的 find 方法实例详解

/**
       *  @brief  Find position of a C string.
       *  @param __s  C string to locate.
       *  @param __pos  Index of character to search from (default 0).
       *  @return  Index of start of first occurrence.
       *
       *  Starting from @a __pos, searches forward for the value of @a
       *  __s within this string.  If found, returns the index where
       *  it begins.  If not found, returns npos.
      */
      size_type
      find(const _CharT* __s, size_type __pos = 0) const
      {
    __glibcxx_requires_string(__s);
    return this->find(__s, __pos, traits_type::length(__s));
      }

View Code
示例代码:

1 void xx_find_2()
 2 {
 3     printf("%s():n", __func__);
 4     const string strSrc = "abcdefghijklmnopqrstuvwxyz";
 5     const char * pStr = NULL;
 6     size_t pos = 0;
 7 
 8     pStr = "cdef";
 9     pos = strSrc.find(pStr);
10     printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
11 
12     pos = strSrc.find(pStr, 0); 
13     printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
14 
15     pos = strSrc.find(pStr, 2); 
16     printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
17 
18     pos = strSrc.find(pStr, 3); 
19     printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
20 }

运行结果:

xx_find_2():
pos =  2    [find]    abcdefghijklmnopqrstuvwxyz
pos =  2    [find]    abcdefghijklmnopqrstuvwxyz
pos =  2    [find]    abcdefghijklmnopqrstuvwxyz
pos = -1    [not find]  abcdefghijklmnopqrstuvwxyz

2.4)第四个定义:size_type find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
C++ string 类的 find 方法实例详解C++ string 类的 find 方法实例详解

/**
       *  @brief  Find position of a character.
       *  @param __c  Character to locate.
       *  @param __pos  Index of character to search from (default 0).
       *  @return  Index of first occurrence.
       *
       *  Starting from @a __pos, searches forward for @a __c within
       *  this string.  If found, returns the index where it was
       *  found.  If not found, returns npos.
      */
      size_type
      find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;

View Code
示例代码:

1 void xx_find_4()
 2 {
 3     printf("%s():n", __func__);
 4     const string strSrc = "abcdefghijklmnopqrstuvwxyz";
 5     int pos = 0;
 6 
 7     char c = 'b';
 8     pos = strSrc.find(c);
 9     printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
10 
11     pos = strSrc.find(c, 0); 
12     printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
13 
14     pos = strSrc.find(c, 1); 
15     printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
16 
17     pos = strSrc.find(c, 2); 
18     printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
19 }

运行结果:

xx_find_4():
pos =  1    [find]    abcdefghijklmnopqrstuvwxyz
pos =  1    [find]    abcdefghijklmnopqrstuvwxyz
pos =  1    [find]    abcdefghijklmnopqrstuvwxyz
pos = -1    [not find]  abcdefghijklmnopqrstuvwxyz

3、rfind()

rfind()函数在basic_string.h中同样有四种定义,定义如下:

3.1)第一个定义:size_type rfind(const basic_string& __str, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
C++ string 类的 find 方法实例详解C++ string 类的 find 方法实例详解

/**
       *  @brief  Find last position of a string.
       *  @param __str  String to locate.
       *  @param __pos  Index of character to search back from (default end).
       *  @return  Index of start of last occurrence.
       *
       *  Starting from @a __pos, searches backward for value of @a
       *  __str within this string.  If found, returns the index where
       *  it begins.  If not found, returns npos.
      */
      size_type
      rfind(const basic_string& __str, size_type __pos = npos) const _GLIBCXX_NOEXCEPT

View Code
实例代码:

1 void xx_rfind_1()
 2 {
 3     //size_type std::basic_string::rfind(const basic_string &__str, size_type __pos);
 4     printf("%s():n", __func__);
 5     const string strSrc = "abcdefghijklmnopqrstuvwxyz";
 6     string strDst;
 7     size_t pos = 0;
 8 
 9     strDst= "uvw";
10     pos = strSrc.rfind(strDst);
11     printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
12 
13     pos = strSrc.rfind(strDst, 25);
14     printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
15 
16     pos = strSrc.rfind(strDst, 20);
17     printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
18 
19     pos = strSrc.rfind(strDst, 19);
20     printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
21 }

运行结果:

xx_rfind_1():
pos = 20    [find]    abcdefghijklmnopqrstuvwxyz
pos = 20    [find]    abcdefghijklmnopqrstuvwxyz
pos = 20    [find]    abcdefghijklmnopqrstuvwxyz
pos = -1    [not find]  abcdefghijklmnopqrstuvwxyz

3.2)第二个定义:size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const;
C++ string 类的 find 方法实例详解C++ string 类的 find 方法实例详解

/**
       *  @brief  Find last position of a C substring.
       *  @param __s  C string to locate.
       *  @param __pos  Index of character to search back from.
       *  @param __n  Number of characters from s to search for.
       *  @return  Index of start of last occurrence.
       *
       *  Starting from @a __pos, searches backward for the first @a
       *  __n characters in @a __s within this string.  If found,
       *  returns the index where it begins.  If not found, returns
       *  npos.
      */
      size_type
      rfind(const _CharT* __s, size_type __pos, size_type __n) const;

View Code
示例代码:

void xx_rfind_2()
{
    //size_type std::basic_string::rfind(const _CharT *__s, size_type __pos, size_type __n);
    printf("%s():n", __func__);
    const string strSrc = "abcdefghijklmnopqrstuvwxyz";
    const char * p = NULL;
    size_t pos = 0;

    p = "uvw";
    pos = strSrc.rfind(p, 25, 2); 
    printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");

    pos = strSrc.rfind(p, 20, 2); 
    printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");

    pos = strSrc.rfind(p, 19, 2); 
    printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
}

运行结果:

xx_rfind_2():
pos = 20    [find]    abcdefghijklmnopqrstuvwxyz
pos = 20    [find]    abcdefghijklmnopqrstuvwxyz
pos = -1    [not find]  abcdefghijklmnopqrstuvwxyz

3.3)第三个定义:size_type rfind(const _CharT* __s, size_type __pos = npos) const
C++ string 类的 find 方法实例详解C++ string 类的 find 方法实例详解

/**
       *  @brief  Find last position of a C string.
       *  @param __s  C string to locate.
       *  @param __pos  Index of character to start search at (default end).
       *  @return  Index of start of  last occurrence.
       *
       *  Starting from @a __pos, searches backward for the value of
       *  @a __s within this string.  If found, returns the index
       *  where it begins.  If not found, returns npos.
      */
      size_type
      rfind(const _CharT* __s, size_type __pos = npos) const

View Code
示例代码:

void xx_rfind_3()
{
    //size_type std::basic_string::rfind(const _CharT *__s, size_type __pos);
    printf("%s():n", __func__);
    const string strSrc = "abcdefghijklmnopqrstuvwxyz";
    const char * p = NULL;
    size_t pos = 0;

    p = "uvw";
    pos = strSrc.rfind(p);
    printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");

    pos = strSrc.rfind(p, 25);
    printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");

    pos = strSrc.rfind(p, 20);
    printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");

    pos = strSrc.rfind(p, 19);
    printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
}

运行结果:

xx_rfind_3():
pos = 20    [find]    abcdefghijklmnopqrstuvwxyz
pos = 20    [find]    abcdefghijklmnopqrstuvwxyz
pos = 20    [find]    abcdefghijklmnopqrstuvwxyz
pos = -1    [not find]  abcdefghijklmnopqrstuvwxyz

3.4)第四个定义: size_type rfind(_CharT __c, size_type __pos = npos) const
C++ string 类的 find 方法实例详解C++ string 类的 find 方法实例详解

/**
       *  @brief  Find last position of a character.
       *  @param __c  Character to locate.
       *  @param __pos  Index of character to search back from (default end).
       *  @return  Index of last occurrence.
       *
       *  Starting from @a __pos, searches backward for @a __c within
       *  this string.  If found, returns the index where it was
       *  found.  If not found, returns npos.
      */
      size_type
      rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;

View Code
示例代码:

void xx_rfind_4()
{
    //size_type std::basic_string::rfind(_CharT __c, size_type __pos);
    printf("%s():n", __func__);
    const string strSrc = "abcdefghijklmnopqrstuvwxyz";
    size_t pos = 0;

    char p = 'u';
    pos = strSrc.rfind(p);
    printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");

    pos = strSrc.rfind(p, 25);
    printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");

    pos = strSrc.rfind(p, 20);
    printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");

    pos = strSrc.rfind(p, 19);
    printf("pos = %2dt[%s]n", pos, -1 == pos ? "not find" : "find");
}

运行结果:

xx_rfind_4():
pos = 20    [find]    abcdefghijklmnopqrstuvwxyz
pos = 20    [find]    abcdefghijklmnopqrstuvwxyz
pos = 20    [find]    abcdefghijklmnopqrstuvwxyz
pos = -1    [not find]  abcdefghijklmnopqrstuvwxyz

4、find_first_of()

find_first_of()函数也有4个定义,函数作用是在当前字符串中查找给定参数中的任意字符的第一次出现位置,有点儿拗口,下面用实例说明。

4.1)第一个定义 size_type find_first_of(const basic_string& __str, size_type __pos = 0)
C++ string 类的 find 方法实例详解C++ string 类的 find 方法实例详解

1       size_type
 2       find_first_of(const basic_string& __str, size_type __pos = 0) const
 3     _GLIBCXX_NOEXCEPT
 4       { return this->find_first_of(__str.data(), __pos, __str.size()); }
 5 
 6       /**
 7        *  @brief  Find position of a character of C substring.
 8        *  @param __s  String containing characters to locate.
 9        *  @param __pos  Index of character to search from.
10        *  @param __n  Number of characters from s to search for.
11        *  @return  Index of first occurrence.
12        *
13        *  Starting from @a __pos, searches forward for one of the
14        *  first @a __n characters of @a __s within this string.  If
15        *  found, returns the index where it was found.  If not found,
16        *  returns npos.
17       */

View Code
示例代码

1 void xx_find_first_of_1()
 2 {
 3     //size_type std::basic_string::find_first_of(const basic_string &__str, size_type __pos);
 4     printf("%s():n", __func__);
 5     const string strSrc = "abcdefghijklmnopqrstuvwxyz";
 6     string strDst;
 7     size_t pos = 0;
 8 
 9     strDst= "cde";
10     pos = strSrc.find_first_of(strDst);//default value of pos is 0
11     printf("pos = %2dt[%s]n", pos, string::npos == pos ? "not find" : "find");
12 
13     pos = strSrc.find_first_of(strDst, 2); 
14     printf("pos = %2dt[%s]n", pos, string::npos == pos ? "not find" : "find");
15 
16     pos = strSrc.find_first_of(strDst, 3); 
17     printf("pos = %2dt[%s]n", pos, string::npos == pos ? "not find" : "find");
18 
19     pos = strSrc.find_first_of(strDst, 4); 
20     printf("pos = %2dt[%s]n", pos, string::npos == pos ? "not find" : "find");
21 
22     pos = strSrc.find_first_of(strDst, 5); 
23     printf("pos = %2dt[%s]n", pos, string::npos == pos ? "not find" : "find");
24 }

运行结果

xx_find_first_of_1():
find [cde]
pos =  2    [find]    abcdefghijklmnopqrstuvwxyz
pos =  2    [find]    abcdefghijklmnopqrstuvwxyz
pos =  3    [find]    abcdefghijklmnopqrstuvwxyz
pos =  4    [find]    abcdefghijklmnopqrstuvwxyz
pos = -1    [not find]    abcdefghijklmnopqrstuvwxyz

原文链接: https://www.cnblogs.com/fengbohello/p/4533328.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/219978

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 上午10:44
下一篇 2023年2月13日 上午10:44

相关推荐