C++ string类:find()和find_first_of()
2016-04-13 09:34
561 查看
容易搞混的两个函数,给出所有重载函数:
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
int find_first_of(const char *s, int pos = 0) const; //从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
共同点:
查找成功时返回所在位置,失败返回string::npos的值,string::npos一般是MAX_INT(即2^32
- 1)
差异:
find(): 查找字符串中第一次出现字符c、字符串s的位置;
find_first_of(): 查找字符串中字符c、字符数组s中任意一个字符第一次出现的位置。
例如:
类似的,还有rfind()和find_last_of(),以及find_first_not_of(), find_last_not_of().
参考文章:
http://blog.csdn.net/richard_123/article/details/18304181
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
int find_first_of(const char *s, int pos = 0) const; //从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
共同点:
查找成功时返回所在位置,失败返回string::npos的值,string::npos一般是MAX_INT(即2^32
- 1)
差异:
find(): 查找字符串中第一次出现字符c、字符串s的位置;
find_first_of(): 查找字符串中字符c、字符数组s中任意一个字符第一次出现的位置。
例如:
string haystack = "helloworld"; string needle = "world"; cout << haystack.find_first_of(needle) << endl; //2, index of first 'l' cout << haystack.find(needle) << endl; //5, index of first "world" needle = ""; cout << haystack.find_first_of(needle) << endl;//string::npos, 因为字符数组s为空,haystack中找不到空字符(区别于'\0') cout << haystack.find(needle) << endl;//0, 空串
类似的,还有rfind()和find_last_of(),以及find_first_not_of(), find_last_not_of().
参考文章:
http://blog.csdn.net/richard_123/article/details/18304181
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- 关于指针的一些事情
- android上改变listView的选中颜色
- String.intern
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- PostgreSQL ERROR: invalid escape string 解决办法
- C++联合体转换成C#结构的实现方法
- 字符串查找 cmd find命令
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题