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

时间:2022-11-12 21:22:00

1、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);

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

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

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

绿色字体:搜索正确


下面逐个解析

  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;

       /**
* @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;

实例代码:

 void xx_find_3()
{
printf("%s():\n", __func__);
const string strSrc = "abcdefghijklmnopqrstuvwxyz";
const char * pStr = NULL;
int pos = ; pStr= "cdefppp";
pos = strSrc.find(pStr, , );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pStr= "cdefppp";
pos = strSrc.find(pStr, , );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pStr= "cdefppp";
pos = strSrc.find(pStr, , );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find");
}

运行结果:

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

  2.2)第二个定义:size_type find(const basic_string& __str, size_type __pos = 0) const

      /**
* @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 = ) const
_GLIBCXX_NOEXCEPT
{ return this->find(__str.data(), __pos, __str.size()); }

实例代码:

void xx_find_1()
{
printf("%s():\n", __func__);
const string strSrc = "abcdefghijklmnopqrstuvwxyz";
string strDst;
size_t pos = ; strDst= "abcdef";
pos = strSrc.find(strDst);
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.find(strDst, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); strDst = "cdef";
pos = strSrc.find(strDst, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.find(strDst, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find");
}

运行结果:

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

  2.3)第三个定义:size_type find(const _CharT* __s, size_type __pos = 0) const

      /**
* @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 = ) const
{
__glibcxx_requires_string(__s);
return this->find(__s, __pos, traits_type::length(__s));
}

示例代码:

 void xx_find_2()
{
printf("%s():\n", __func__);
const string strSrc = "abcdefghijklmnopqrstuvwxyz";
const char * pStr = NULL;
size_t pos = ; pStr = "cdef";
pos = strSrc.find(pStr);
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.find(pStr, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.find(pStr, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.find(pStr, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find");
}

运行结果:

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

  2.4)第四个定义:size_type find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;

      /**
* @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 = ) const _GLIBCXX_NOEXCEPT;

示例代码:

 void xx_find_4()
{
printf("%s():\n", __func__);
const string strSrc = "abcdefghijklmnopqrstuvwxyz";
int pos = ; char c = 'b';
pos = strSrc.find(c);
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.find(c, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.find(c, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.find(c, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find");
}

运行结果:

xx_find_4():
pos = [find]    abcdefghijklmnopqrstuvwxyz
pos = [find]    abcdefghijklmnopqrstuvwxyz
pos = [find]    abcdefghijklmnopqrstuvwxyz
pos = - [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

      /**
* @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

  实例代码:

 void xx_rfind_1()
{
//size_type std::basic_string::rfind(const basic_string &__str, size_type __pos);
printf("%s():\n", __func__);
const string strSrc = "abcdefghijklmnopqrstuvwxyz";
string strDst;
size_t pos = ; strDst= "uvw";
pos = strSrc.rfind(strDst);
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.rfind(strDst, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.rfind(strDst, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.rfind(strDst, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find");
}

  运行结果:

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

  3.2)第二个定义:size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const;

      /**
* @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;

  示例代码:

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 = ; p = "uvw";
pos = strSrc.rfind(p, , );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.rfind(p, , );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.rfind(p, , );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find");
}

  运行结果:

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

  3.3)第三个定义:size_type rfind(const _CharT* __s, size_type __pos = npos) const

      /**
* @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

示例代码:

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 = ; p = "uvw";
pos = strSrc.rfind(p);
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.rfind(p, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.rfind(p, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.rfind(p, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find");
}

运行结果:

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

  3.4)第四个定义: size_type rfind(_CharT __c, size_type __pos = npos) const

      /**
* @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;

示例代码:

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 = ; char p = 'u';
pos = strSrc.rfind(p);
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.rfind(p, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.rfind(p, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find"); pos = strSrc.rfind(p, );
printf("pos = %2d\t[%s]\n", pos, - == pos ? "not find" : "find");
}

运行结果:

xx_rfind_4():
pos = [find]    abcdefghijklmnopqrstuvwxyz
pos = [find]    abcdefghijklmnopqrstuvwxyz
pos = [find]    abcdefghijklmnopqrstuvwxyz
pos = - [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)

       size_type
find_first_of(const basic_string& __str, size_type __pos = ) const
_GLIBCXX_NOEXCEPT
{ return this->find_first_of(__str.data(), __pos, __str.size()); } /**
* @brief Find position of a character of C substring.
* @param __s String containing characters to locate.
* @param __pos Index of character to search from.
* @param __n Number of characters from s to search for.
* @return Index of first occurrence.
*
* Starting from @a __pos, searches forward for one of the
* first @a __n characters of @a __s within this string. If
* found, returns the index where it was found. If not found,
* returns npos.
*/

示例代码

 void xx_find_first_of_1()
{
//size_type std::basic_string::find_first_of(const basic_string &__str, size_type __pos);
printf("%s():\n", __func__);
const string strSrc = "abcdefghijklmnopqrstuvwxyz";
string strDst;
size_t pos = ; strDst= "cde";
pos = strSrc.find_first_of(strDst);//default value of pos is 0
printf("pos = %2d\t[%s]\n", pos, string::npos == pos ? "not find" : "find"); pos = strSrc.find_first_of(strDst, );
printf("pos = %2d\t[%s]\n", pos, string::npos == pos ? "not find" : "find"); pos = strSrc.find_first_of(strDst, );
printf("pos = %2d\t[%s]\n", pos, string::npos == pos ? "not find" : "find"); pos = strSrc.find_first_of(strDst, );
printf("pos = %2d\t[%s]\n", pos, string::npos == pos ? "not find" : "find"); pos = strSrc.find_first_of(strDst, );
printf("pos = %2d\t[%s]\n", pos, string::npos == pos ? "not find" : "find");
}

运行结果

xx_find_first_of_1():
find [cde]
pos = [find] abcdefghijklmnopqrstuvwxyz
pos = [find] abcdefghijklmnopqrstuvwxyz
pos = [find] abcdefghijklmnopqrstuvwxyz
pos = [find] abcdefghijklmnopqrstuvwxyz
pos = - [not find] abcdefghijklmnopqrstuvwxyz

C++ string 类的 find 方法实例详解的更多相关文章

  1. C#操作SQLite方法实例详解

    用 C# 访问 SQLite 入门(1) CC++C#SQLiteFirefox  用 C# 访问 SQLite 入门 (1) SQLite 在 VS C# 环境下的开发,网上已经有很多教程.我也是从 ...

  2. 类的特殊方法"__call__"详解

    1. __call__ 当执行对象名+括号时, 会自动执行类中的"__call__"方法, 怎么用? class A: def __init__(self, name): self ...

  3. MySQL死锁问题分析及解决方法实例详解(转)

      出处:http://www.jb51.net/article/51508.htm MySQL死锁问题是很多程序员在项目开发中常遇到的问题,现就MySQL死锁及解决方法详解如下: 1.MySQL常用 ...

  4. php中自动加载类_autoload()和spl_autoload_register()实例详解

    一._autoload 自动加载类:当我们实例化一个未定义的类时,就会触此函数.到了php7.1以后版本不支持此函数好像抛弃了 新建一个类文件名字自己随便去:news类在auto.php文件里面去实例 ...

  5. PHP类和对象函数实例详解

    1. interface_exists.class_exists.method_exists和property_exists: 顾名思义,从以上几个函数的命名便可以猜出几分他们的功能.我想这也是我随着 ...

  6. ECMAScript5中新增的Array方法实例详解

    ECMAScript5标准发布于2009年12月3日,它带来了一些新的,改善现有的Array数组操作的方法.(注意兼容性) 在ES5中,一共有9个Array方法:http://kangax.githu ...

  7. 类的特殊方法"__new__"详解

    上代码! class A: def __new__(cls, *args, **kwargs): obj = super().__new__(cls) print("__new__ &quo ...

  8. JavaScript学习笔记-实例详解-类(一)

    实例详解-类(一): //每个javascript函数(除了bind())都自动拥有一个prototype对象// 在未添加属性或重写prototype对象之前,它只包含唯一一个不可枚举属性const ...

  9. 集合类 Contains 方法 深入详解 与接口的实例

    .Net 相等性:集合类 Contains 方法 深入详解 http://www.cnblogs.com/ldp615/archive/2009/09/05/1560791.html 1.接口的概念及 ...

随机推荐

  1. GROUP BY的扩展

    GROUP BY的扩展主要包括ROLLUP,CUBE,GROUPING SETS三种形式. ROLLUP rollup相对于简单的分组合计增加了小计和合计,解释起来会比较抽象,下面我们来看看具体事例. ...

  2. js中replace的回调函数使用。

    这只是一个小问题,但是之前并没有发现.这个问题就是replace的第二个函数是支持回调函数的. var ext = new RegExp('f','g'); 1.str.replace(ext ,1) ...

  3. Mysql 中 text类型和 blog类型的异同

    MySQL存在text和blob: (1)相同 在TEXT或BLOB列的存储或检索过程中,不存在大小写转换,当未运行在严格模式时,如果你为BLOB或TEXT列分配一个超过该列类型的最大长度的值值,值被 ...

  4. ARM处理器全解析:A8/A9/A15都是什么?

    前不久ARM正式宣布推出新款ARMv8架构的Cortex-A50处理器系列产品,以此来扩大ARM在高性能与低功耗领域的领先地位,进一步抢占移动终端市场份额.Cortex-A50是继Cortex-A15 ...

  5. vijosP1901学姐的钱包

    题目:https://vijos.org/p/1901 题解:这题比较有意思. 经过一番思考之后我想出了下面的算法: 我们反着来推,按i从大到小 f[i]表示从>=m到 i 需要多长时间,则如果 ...

  6. C#的抽象类和接口,区别与相似

        一.抽象类:抽象类是特殊的类,只是不能被实例化:除此以外,具有类的其他特性:重要的是抽象类可以包括抽象方法,这是普通类所不能的.抽象方法只能声明于抽象类中,且不包含任何实现,派生类必须覆盖它们 ...

  7. new和malloc

    1.申请的内存所在位置 *存储区(free store)是C++基于new操作符的一个抽象概念,凡是new进行内存申请,该内存为*存储区.堆是操作系统中的术语,是操作系统所维护的一块特殊内存,用于 ...

  8. 【学习】数据处理基础知识(缺失值处理)【pandas】

    缺失数据(missing data)大部分数据分析应用中非常常见.pd设计目标之一就是让缺失数据的处理任务尽量轻松. pd 使用浮点值NaN(Not a Number) 表示浮点和非浮点数组中的缺失数 ...

  9. Cannot obtain the required interface ("IID_IDBCreateCommand") from OLE DB provider "OraOLEDB.Oracle" for linked server xxxx

      今天遇到了一个关于LINKED SERVER查询报错的案例,链接服务器链接ORACLE数据库,测试没有错误,但是执行脚本的时候,报如下错误: Msg 7399, Level 16, State 1 ...

  10. _skill,_skill_category

    _skill,_skill_category -- 自定义商业技能-- 小技巧:配合增加自定义商业技能._add skill [ID _skill `skillId`商业技能ID `skillIcon ...