string 类成员函数 find() / find_first_of() / find_last_of() 的用法详解
2017-08-24 14:32
639 查看
一:find()函数用法详解
函数原型:
size_t find(const string &str, size_t pos = 0) const
size_t find (const char *s, size_t pos, size_t n) const
size_t find (const char *s, size_t pos = 0) const
size_t find (char c,size_t pose = 0) const
举例说明:
string s1 = "how to use the find";
string s2 = "use";
那么,我们既可以在 s1 中查找 s2 中的内容,也可以在 s1 自己中查找某个字符或者字符串。最终都是返回查找到的字符(串)在s1中的首地址。
1) size_t found = s1.find(s2);
cout << found;
结果为7。也就是‘u’在 s1 中的索引,注意,别忽略了空格。如果不存在这个字符串,那么返回的是一个很大的数。
2)size_t found = s1.find(s2, 3);
cout
<< found;
结果为7。也就是‘u’在 s1 中的索引。此时,是从s1 的第 3 个字符开始寻找
3)size_t found = s1.find("to"); cout << found;
结果为4。如果这样,size_t found = s1.find("to"); cout
<< found; 结果也是4. 但是如果这样:size_t found = s1.find("tow"); cout << found; 结果为一个很大的值
4)size_t found = s1.find("use",2,2); cout << found;
结果为 7 。表示从 s1 中第二个字符开始寻找 “use” 的前两个字符。此时,只要 “use” 前两个字符满足在 s1 中即可。如果这样:
size_t found = s1.find("usq",2,2); 结果仍然是对的,为7.
二:find_first_of()函数用法详解
函数原型:
size_t find_first_of(const string &str, size_t pos = 0) const
size_t find_first_of (const char *s, size_t pos, size_t n) const
size_t find_first_of (const char *s, size_t pos = 0) const
size_t find_first_of (char c,size_t pose = 0) const
单单从函数的形参来看,基本是类似的,但是,与 find() 函数不同的是,如果在 s1 中查找 s2 ,如果s1 中含有 s2 的任何字符,就查找成功。
比如,size_t found = s1.find_first_of("usetow");
cout << found;
输出结果是 1 。为什么是1 呢?其实这个函数实际返回的是,“usetow” 中第一个出现在 s1 中的那个字符的位置。显然,字符 ‘o’ 是最早的那个出现在 s1 中的。
那如果这样:size_t found = s1.find_first_of("usetow",6);
输出结果就是 7。因为它是从编号为6的字符(即空格)开始查找,找到的是‘u’
函数原型:
size_t find(const string &str, size_t pos = 0) const
size_t find (const char *s, size_t pos, size_t n) const
size_t find (const char *s, size_t pos = 0) const
size_t find (char c,size_t pose = 0) const
举例说明:
string s1 = "how to use the find";
string s2 = "use";
那么,我们既可以在 s1 中查找 s2 中的内容,也可以在 s1 自己中查找某个字符或者字符串。最终都是返回查找到的字符(串)在s1中的首地址。
1) size_t found = s1.find(s2);
cout << found;
结果为7。也就是‘u’在 s1 中的索引,注意,别忽略了空格。如果不存在这个字符串,那么返回的是一个很大的数。
2)size_t found = s1.find(s2, 3);
cout
<< found;
结果为7。也就是‘u’在 s1 中的索引。此时,是从s1 的第 3 个字符开始寻找
3)size_t found = s1.find("to"); cout << found;
结果为4。如果这样,size_t found = s1.find("to"); cout
<< found; 结果也是4. 但是如果这样:size_t found = s1.find("tow"); cout << found; 结果为一个很大的值
4)size_t found = s1.find("use",2,2); cout << found;
结果为 7 。表示从 s1 中第二个字符开始寻找 “use” 的前两个字符。此时,只要 “use” 前两个字符满足在 s1 中即可。如果这样:
size_t found = s1.find("usq",2,2); 结果仍然是对的,为7.
二:find_first_of()函数用法详解
函数原型:
size_t find_first_of(const string &str, size_t pos = 0) const
size_t find_first_of (const char *s, size_t pos, size_t n) const
size_t find_first_of (const char *s, size_t pos = 0) const
size_t find_first_of (char c,size_t pose = 0) const
单单从函数的形参来看,基本是类似的,但是,与 find() 函数不同的是,如果在 s1 中查找 s2 ,如果s1 中含有 s2 的任何字符,就查找成功。
比如,size_t found = s1.find_first_of("usetow");
cout << found;
输出结果是 1 。为什么是1 呢?其实这个函数实际返回的是,“usetow” 中第一个出现在 s1 中的那个字符的位置。显然,字符 ‘o’ 是最早的那个出现在 s1 中的。
那如果这样:size_t found = s1.find_first_of("usetow",6);
输出结果就是 7。因为它是从编号为6的字符(即空格)开始查找,找到的是‘u’
相关文章推荐
- string类成员函数find/find_first_of用法详解
- 20170604_string中的函数find_first_of()和 find_last_of()
- string 中find_first_of()函数用法例题
- 字符串处理函数find_first_of和find_last_of的使用
- string里的IndexOf、LastIndexOf、Substring的详解(意义和用法)
- CString中find_first_of和find_last_of()函数
- string c++详解 find_first_not_of() find_first_of()
- C++ 标准模板库学习之 string 类 详解 (一) 将一个句子中每个单词的单词字母顺序翻转 关于npos find_first_not_of find_first_of getline
- 函数find_first_of()和 find_last_of()
- string里的IndexOf、LastIndexOf、Substring的详解(意义和用法)
- (转帖)如何将字符串前后的空白去除? (使用string.find_first_not_of, string.find_last_not_of) (C/C++)
- string里的IndexOf、LastIndexOf、Substring的详解(意义和用法)
- std::string 类中find_first_not_of ()函数
- string里的IndexOf、LastIndexOf、Substring的详解(意义和用法)
- string里的IndexOf、LastIndexOf、Substring的详解(意义和用法)
- C++中string.find()函数,string.find_first_of函数与string::npos
- string find_last_of 用法
- C++中string类的成员函数find_first_not_of()
- string里的IndexOf、LastIndexOf、Substring的详解(意义和用法)
- (原創) 如何将字符串前后的空白去除? (使用string.find_first_not_of, string.find_last_not_of) (C/C++)