关于C++里的find查找函数的简单说明

关于C++里的find查找函数
本人小白做一下简单的小结。。
先给一下我用的代码这些代码吧:
<span style="color:#ff0000;">#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    string s1="love our and you our";
    string s2="our";
    string s3="myself";
    string s4="love";
    string s5="or";
    string s6="and";
    cout<<s1.find(s2)<<endl;;
    cout<<s1.find(s3)<<endl;
    cout<<s1.find(s4)<<endl<<s1.find(s5)<<endl;
    cout<<s1.find_first_of(s6)<<" "<<s1.find_last_of(s6)<<endl;
    cout<<s1.find(s2)<<" "<<s1.rfind(s2)<<endl;
    int a[6]={1,2,3,4,5,6};
    cout<<find(a,a+6,3)<<endl;
    cout<<*find(a,a+6,3)<<endl;
    char s[12]={"love myself"};
    cout<<find(s,s+12,'g')<<endl;
    cout<<find(s,s+12,'m')<<endl;
    return 0;
}</span><span style="color:#000099;">
</span>
1.首先说一下 在C++中string自带的find函数,其使用规范是  s1.find(s2), 意思就是在S1串中寻找是否有S2,如果有则返回串S1中第一次出现串S2的第一个字符,如果没有会返回4294967295这个值,这种查找是一种完全查找,就相当于char类型下的字符串下查找一样
比如说
<span style="color:#ff0000;">cout<<s1.find(s2)<<endl;;
cout<<s1.find(s3)<<endl;</span>
<span style="color:#ff0000;">cout<<s1.find(s4)<<endl<<s1.find(s5)<<endl;</span>
提到过的这些代码输出的便是
5
4294967295
0
4294967295
另比较特殊的变形.
s1.find_first_of(s6)      s1.find_last_of(s6)
这种的是不完全匹配,及查找到与串S2第一个字符相同时返回他的位置
前者是从前往后查找,后者是从后往前找找到的
这个位置输出的是:
9 11
另外不多见的还有 rfind,他是与find相对应的 从后往前找的
测试实例,提供的代码自行测试.


2.其次是包含在 algorithm 头文件中的find函数,他的使用方法同sort是相类似的
如所提供代码所示
<span style="color:#ff0000;">    int a[6]={1,2,3,4,5,6};
    cout<<find(a,a+6,3)<<endl;
    cout<<*find(a,a+6,3)<<endl;
    char s[12]={"love myself"};
    cout<<find(s,s+12,'g')<<endl;
    cout<<find(s,s+12,'m')<<endl;</span>
总之就是find(a+begin,a+end)这种格式,他的返回值是指针
像第二行代码输出的就是查找到的位置 0x28fec4,第三行代码就是他这个数
没查找到应该就是随机赋值了,具体,自行测试





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值