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.接口的概念及 ...
随机推荐
- 搜索引擎爬虫技术研究(爬虫框架)-WebCollector
一.简介: https://github.com/CrawlScript/WebCollector/blob/master/README.zh-cn.md 二.使用: <dependency&g ...
- linux SecureCRT ssh key认证登陆
转自:http://blog.chinaunix.net/uid-20639775-id-3207171.html 通过SecureCRT创建key登录认证 一.生成公钥/密钥对 使用SecureCR ...
- 使用AStyle进行代码格式化
转自:http://www.cnblogs.com/JerryTian/archive/2012/09/20/AStyle.html 在日常的编码当中,大家经常要遵照一些设计规范,如命名规则.代码格式 ...
- Printf()输出格式控制(转)
int printf(const char *format,[argument]); format 参数输出的格式,定义格式为: %[flags][width][.perc] [F|N|h|l]typ ...
- 函数fseek() 用法(转)
在阅读代码时,遇到了很早之前用过的fseek(),很久没有用了,有点陌生,写出来以便下次查阅. 函数功能是把文件指针指向文件的开头,需要包含头文件stdio.h fseek 函数名: fseek ...
- 介绍linux下vi命令的使用
功能最强大的编辑器之一——vivi是所有UNIX系统都会提供的屏幕编辑器,它提供了一个视窗设备,通过它可以编辑文件.当然,对UNIX系统略有所知的人,或多或少都觉得vi超级难用,但vi是最基本的编辑器 ...
- 智能车学习(十四)——K60单片机GPIO学习
一.头文件: #ifndef __MK60_GPIO_H__ #define __MK60_GPIO_H__ #include "MK60_gpio_cfg.h" /* * 定义管 ...
- Android拓展系列(10)--使用Android Studio阅读整个Android源码
之前一直在windows下用source insight阅读android源码,效果非常好.后来远程异地服务器,网络限制,一直用ssh + vim,现在主要还是以这种方式.最近发现一个不错的东西(早就 ...
- POJ 2201 Cartesian Tree ——笛卡尔树
[题目分析] 构造一颗笛卡尔树,然后输出这棵树即可. 首先进行排序,然后用一个栈维护最右的树的节点信息,插入的时候按照第二关键字去找,找到之后插入,下面的树成为它的左子树即可. 然后插入分三种情况讨论 ...
- vs2008/2010安装无法打开数据文件解决方案
本人在安装VS2008或2010时,在开始的第一个页面(进度条大约加载到75%左右),提示“无法打开数据文件 'C:/Documents and Settings/Administrator/Loca ...