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. Thinkphp 解决写入配置文件的方法

    在/Application/Common/Common创建function.php,然后添加以下代码: <?php /** * [writeArr 写入配置文件方法] * @param [typ ...

  2. Sizeof运算符小结

    以下内容援引自<C Primer Plus>中文版第五版Page95 Sizeof运算符以字节为单位返回其操作数的大小.(在C中,1个字节被定义为char类型所占用空间的大小.在过去,1个 ...

  3. Java学习随笔1:Java是值传递还是引用传递?

    Java always passes arguments by value NOT by reference. Let me explain this through an example: publ ...

  4. MIT 6.828 JOS学习笔记2. Lab 1 Part 1.2: PC bootstrap

    Lab 1 Part 1: PC bootstrap 我们继续~ PC机的物理地址空间 这一节我们将深入的探究到底PC是如何启动的.首先我们看一下通常一个PC的物理地址空间是如何布局的:        ...

  5. 用super daemon xinetd进行安全配置

    xinetd 可以进行安全性或者是其他的管理机制的控制,这些控制手段都可以让我们的服务更为安全,资源管理更为合理.一些对客户端开放较多权限的服务(例如telnet)或者本身不带有管理机制的服务就可以通 ...

  6. 持续集成基础-Jenkins(二)-搭建Jenkins环境和配置第一个Job

    安装方式一(直接启动): 1.下载最新的版本(一个 WAR 文件).Jenkins官方网址: http://Jenkins-ci.org/ 2.运行 java -jar jenkins.war(需要运 ...

  7. C# 词法分析器(一)词法分析介绍 update 2014.1.8

    系列导航 (一)词法分析介绍 (二)输入缓冲和代码定位 (三)正则表达式 (四)构造 NFA (五)转换 DFA (六)构造词法分析器 (七)总结 虽然文章的标题是词法分析,但首先还是要从编译原理说开 ...

  8. C# 词法分析器(七)总结

    系列导航 (一)词法分析介绍 (二)输入缓冲和代码定位 (三)正则表达式 (四)构造 NFA (五)转换 DFA (六)构造词法分析器 (七)总结 在之前的六篇文章中,我比较详细的介绍了与词法分析器相 ...

  9. Redis 事务总结

        特点: 对单个客户端可以执行连续性事务(在一个线程内): 执行命令要排队: mutil类似begin trans; exec 类似 commit; discard 用于放弃事务: watch ...

  10. 数据结构:后缀自动机 WJMZBMR讲稿的整理和注释

    链接放在这里,有点难理解,至少我个人是的. 后缀自动机是一种有限状态自动机,其功能是识别字符串是否是母串的后缀.它能解决的问题当然不仅仅是判断是不是后缀这种事,跟字符串的连续子串有关的问题都可以往这个 ...