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.接口的概念及 ...
随机推荐
- maven错误解决一:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile)
解决方法是将 jre的目录在 window->Preferences 里修改java installed里的jre目录改为jdk目录即可. 原因是在jre目录下不存在tools.jar.
- js 控制展开折叠 div html dom
js 控制展开折叠 div html dom <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ...
- WPF MVVM模式下实现ListView下拉显示更多内容
在手机App中,如果有一个展示信息的列表,通常会展示很少一部分,当用户滑动到列表底部时,再加载更多内容.这样有两个好处,提高程序性能,减少网络流量.这篇博客中,将介绍如何在WPF ListView中实 ...
- hdu 4763 kmp ***
找AEAEA形式的字符串最长的A长度,E可以为空 只可意会,不可言传,懂kmp即可 #include <stdio.h> #include <string.h> #includ ...
- web.xml中同一servlet/filter配置多个url-pattern
转自:http://blog.sina.com.cn/s/blog_4c2c2a0c0100dh67.html 若你的servlet要多个地址,或你的filter需要过滤不同的url如有*.jsp,* ...
- 有关servlet初学者的资源和建议
四天来学习servlet是很痛苦的经历,其实可以不必要这么痛苦,关键是一定要学会冷静的分析问题与解决问题,要不言学习也没有那么多的乐趣.初学java刚满15天. 首先对于资源来说建议先读一点点的PPT ...
- Fragment 操作原理
fragment 本质 fragment 本质上是 view 的容器和控制器,fragment 是 activity 的碎片. activity 是什么呢?activity 是四大组件之一,因为 ...
- 近实时运算的利器---presto在公司实践
1.起因 公司hadoop集群里的datanonde和tasktracker节点负载主要集中于晚上到凌晨,平日工作时间负载不是很高.但在工作时间内,公司业务人员有实时查询需求,现在主要 借助于hive ...
- Android拓展系列(12)--使用Gradle发布aar项目到JCenter仓库
目的 发布自己的android library(也就是aar)到公共的jcenter仓库,所有的人都能用gradle最简单的方式引用. 为什么选择jcenter,它兼容maven,而且支持更多形式仓库 ...
- hdu 并查集分类(待续)
hdu 1829 A Bug's Life 题目大意: 给你n个动物,输入m行a,b,表示a和b应该是异性的,要你判断是否有同性恋. 并查集中,1到n代表应性别,n+1到2n代表一个性别,合并一下,判 ...