通常来说,find函数用于寻找某个序列的在string中第一次出现的位置。
find函数有以下四种重载版本:
size_t find (const string& str, size_t pos = 0) const noexceptsize_t find (const char* s, size_t pos = 0) const
size_t find (const char* s, size_t pos, <a href="https://www.baidu.com/s?wd=size_type&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLP1RLPhRYPW6LnvuBnWnz0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En163nHD1nWcs" target="_blank" class="baidu-highlight">size_type</a> 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。
c语言find函数的用法详解C语言之find()函数
find函数用于查找数组中的某一个指定元素的位置。
比如:有一个数组[0, 0, 5, 4, 4];
问:元素5的在什么位置,find函数 返回值 为 2;
find (数组名 + 起始查找元素的位置, 数组名 + 结束查找的元素位置, 想要查找的元素)
直接上代码:
#include <iostream>
#include <vector>
#include <algorithm>//注意要包含该头文件
using namespace std
int main()
{
int nums[] = { 3, 1, 4, 1, 5, 9 }
int num_to_find = 5
int start = 0
int end = 5
int* result = find( nums + start, nums + end, num_to_find )
if( result == nums + end )
{
cout<<"Did not find any number matching " <<num_to_find <<endl
}
else
{
cout<<"Found a matching number: " <<*result <<endl
}
return 0
}