string 里的 find
来源:互联网 发布:上海网络推广 编辑:程序博客网 时间:2024/05/21 13:54
通常来说,find函数用于寻找某个序列的在string中第一次出现的位置。
find函数有以下四种重载版本:
1
2
3
4
size_t
find (
const
string& str,
size_t
pos = 0)
const
noexcept;
size_t
find (
const
char
* s,
size_t
pos = 0)
const
;
size_t
find (
const
char
* s,
size_t
pos, size_type n)
const
;
size_t
find (
char
c,
size_t
pos = 0)
const
noexcept;
参数说明:
str/s/c:要寻找的序列,可以是字符串(版本1),也可以是字符串字面值或者说C风格字符串(版本2、3,在版本3中,所寻找的序列是从s[0]开始的前n个字符),也可以是字符(版本4)。
pos:从string的pos位置开始寻找(注意第一个位置是0)。
函数返回序列第一次出现的位置,如果没有找到则返回string::npos。
样例:(摘自cplusplus.com)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream> // std::cout
#include <string> // std::string
int
main ()
{
std::string str (
"There are two needles in this haystack with needles."
);
std::string str2 (
"needle"
);
// different member versions of find in the same order as above:
std::
size_t
found = str.find(str2);
if
(found!=std::string::npos)
std::cout <<
"first 'needle' found at: "
<< found <<
'\n'
;
found=str.find(
"needles are small"
,found+1,6);
if
(found!=std::string::npos)
std::cout <<
"second 'needle' found at: "
<< found <<
'\n'
;
found=str.find(
"haystack"
);
if
(found!=std::string::npos)
std::cout <<
"'haystack' also found at: "
<< found <<
'\n'
;
found=str.find(
'.'
);
if
(found!=std::string::npos)
std::cout <<
"Period found at: "
<< found <<
'\n'
;
// let's replace the first needle:
str.replace(str.find(str2),str2.length(),
"preposition"
);
std::cout << str <<
'\n'
;
return
0;
}
标准输出结果:
first 'needle' found at: 14
second 'needle' found at: 44
'haystack' also found at: 30
Period found at: 51
There are two prepositions in this haystack with needles.
阅读全文
0 0
- string 里的 find
- string的find操作
- string的find函数
- VC里的String
- Python里的String
- List里find的使用。
- C++里的find()函数
- string::find()的用法总结
- C++ string find的误用
- string find的用法详解
- C++里string的用法
- java里的String Pool
- java 里特殊的String
- c++里的string类
- C++里的string类初窥
- find -perm 里的 + -的含义
- string find
- string find
- 11. Container With Most Water
- 编程求和:s=a+aa+aaa+aaaa+ ??+aaaa?aaa(n个),其中a为1~9中的一个数字。 提示:若第一项为a , 以后每一项由前一项乘以10加上a递推得到,然后求和。 #incl
- 21.post与get的区别
- 算法:动态申请数组(new)后将数组所有元素置零(初始化数组)的方法(炒鸡简单)(C++语言)
- web。http简介
- string 里的 find
- 编译原理5、6
- 小程序完成后台连接
- input:-webkit-autofill 导致chrome的输入框背景颜色变成黄色
- 简易的遮罩层效果
- json-api
- 分布式事务方案
- 在MAC上安装Libvirt-python
- 谈SPFA解差分约束最大值最小值的原理...