C++ string 类的 find 方法实例详解
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 方法实例详解的更多相关文章
- C#操作SQLite方法实例详解
用 C# 访问 SQLite 入门(1) CC++C#SQLiteFirefox 用 C# 访问 SQLite 入门 (1) SQLite 在 VS C# 环境下的开发,网上已经有很多教程.我也是从 ...
- 类的特殊方法"__call__"详解
1. __call__ 当执行对象名+括号时, 会自动执行类中的"__call__"方法, 怎么用? class A: def __init__(self, name): self ...
- MySQL死锁问题分析及解决方法实例详解(转)
出处:http://www.jb51.net/article/51508.htm MySQL死锁问题是很多程序员在项目开发中常遇到的问题,现就MySQL死锁及解决方法详解如下: 1.MySQL常用 ...
- php中自动加载类_autoload()和spl_autoload_register()实例详解
一._autoload 自动加载类:当我们实例化一个未定义的类时,就会触此函数.到了php7.1以后版本不支持此函数好像抛弃了 新建一个类文件名字自己随便去:news类在auto.php文件里面去实例 ...
- PHP类和对象函数实例详解
1. interface_exists.class_exists.method_exists和property_exists: 顾名思义,从以上几个函数的命名便可以猜出几分他们的功能.我想这也是我随着 ...
- ECMAScript5中新增的Array方法实例详解
ECMAScript5标准发布于2009年12月3日,它带来了一些新的,改善现有的Array数组操作的方法.(注意兼容性) 在ES5中,一共有9个Array方法:http://kangax.githu ...
- 类的特殊方法"__new__"详解
上代码! class A: def __new__(cls, *args, **kwargs): obj = super().__new__(cls) print("__new__ &quo ...
- JavaScript学习笔记-实例详解-类(一)
实例详解-类(一): //每个javascript函数(除了bind())都自动拥有一个prototype对象// 在未添加属性或重写prototype对象之前,它只包含唯一一个不可枚举属性const ...
- 集合类 Contains 方法 深入详解 与接口的实例
.Net 相等性:集合类 Contains 方法 深入详解 http://www.cnblogs.com/ldp615/archive/2009/09/05/1560791.html 1.接口的概念及 ...
随机推荐
- apache lucene solr 官网历史版本下载地址
官网上一般只提供最新版本的下载,下面两个链接为所有历史版本的下载地址: lucene地址:archive.apache.org/dist/lucene/java/ solr地址:archive.apa ...
- Oracle12c client安裝報錯[INS-20802] Oracle Net Configuration Assistant failed完美解決
Doc ID 2082662.1 1.錯誤碼 Installation Of Oracle Client 12.1.0.2.0 (32-bit) Fails With An Error Message ...
- Bootstrap看厌了?试试Metro UI CSS吧
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:Bootstrap作为一款超级流行的前端框架,已经成为很多人的首选,不过有时未免有点审 ...
- 一个简单的Object Hook的例子(win7 32bit)
Object Hook简单的来说就是Hook对象,这里拿看雪上的一个例子,因为是在win7 32位上的,有些地方做了些修改. _OBJECT_HEADER: kd> dt _OBJECT_HEA ...
- 数字信号处理实验(四)——数字滤波器结构
一.滤波器结构 1.IIR滤波器 (1)系统函数 (2)差分方程 (3)级联形式: (4)并联形式 2.FIR滤波器 (1)系统函数 (2)差分方程 (3)级联形式: (4 ...
- 【java基础】选择排序and冒泡排序
前言 : 今天学习的是J2SE视频里的第五章,数组部分,它里面留了一个经典的作业,就是让我们去从1倒9按一定规格排序,这让我想起了学习vb的时候最最让我头疼的两种排序方法,选择排序法 和 冒泡排序法. ...
- AS项目转到eclipse中方法
手工改,1.在eclipse 上新建一个空的项目;2.点击android studio 中的android 视图, a.替换as 中的AndroidManifest.xml -> ...
- poj 1651 Multiplication Puzzle (区间dp)
题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...
- PHP 设置代码执行时间
<?php ini_set('max_execution_time', '0'); set_time_limit(0); ?>
- 费用流 ZOJ 3933 Team Formation
题目链接 题意:两个队伍,有一些边相连,问最大组对数以及最多女生数量 分析:费用流模板题,设置两个超级源点和汇点,边的容量为1,费用为男生数量.建边不能重复建边否则会T.zkw费用流在稠密图跑得快,普 ...