stl 库里的find函数用法

本文详细介绍了C++中字符串的各种操作方法,包括查找指定子串的位置、获取子串首次及末次出现的位置、从特定位置开始查找子串、查找所有出现的位置以及反向查找等实用技巧,并通过两个实例演示了如何利用这些方法解决实际问题。

转载自大佬:https://www.cnblogs.com/wkfvawl/p/9452869.html

 

1.string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos。(返回值可以看成是一个int型的数)

复制代码
 1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 using namespace std;  5 int main()  6 {  7 ////find函数返回类型 size_type  8 string s("1a2b3c4d5e6f7jkg8h9i1a2b3c4d5e6f7g8ha9i");  9 string flag; 10 string::size_type position; 11 //find 函数 返回jk 在s 中的下标位置 12 position = s.find("jk"); 13 if (position != s.npos) //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295, 14  { 15 printf("position is : %d\n" ,position); 16  } 17 else 18  { 19 printf("Not found the flag\n"); 20  } 21 }
复制代码

 

2.返回子串出现在母串中的首次出现的位置,和最后一次出现的位置。

1  flag = "c";
2 position = s.find_first_of(flag); 3 printf("s.find_first_of(flag) is :%d\n",position); 4 position = s.find_last_of(flag); 5 printf("s.find_last_of(flag) is :%d\n",position);

 

 

3.查找某一给定位置后的子串的位置

1  //从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标
2     position=s.find("b",5); 3 cout<<"s.find(b,5) is : "<<position<<endl;

 

4.查找所有子串在母串中出现的位置

复制代码
//查找s 中flag 出现的所有位置。
    flag="a"; position=0; int i=1; while((position=s.find_first_of(flag,position))!=string::npos) { cout<<"position "<<i<<" : "<<position<<endl; position++; i++; }
复制代码

 

5.反向查找子串在母串中出现的位置,通常我们可以这样来使用,当正向查找与反向查找得到的位置不相同说明子串不唯一。

1     //反向查找,flag 在s 中最后出现的位置
2     flag="3"; 3 position=s.rfind (flag); 4 printf("s.rfind (flag) :%d\n",position);

 

 

例题:1.给出一个字符串,串中会出现有人名,找到一个只有一个人名的字符串。

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 vector<string> s;  4 int main()  5 {  6 s.push_back("Danil");  7 s.push_back("Olya");  8 s.push_back("Slava");  9 s.push_back("Ann"); 10 s.push_back("Nikita");///建立动态数组 11 string a; 12 cin>>a; 13 int res = 0; 14 for(int i = 0; i < 5; i++) 15  { 16 if(a.find(s[i]) != a.npos) 17  { 18 res++; 19 if(a.rfind(s[i]) != a.find(s[i]))///一个字符中出现多个一样的名字 20  { 21 res++; 22  } 23  } 24  } 25 if(res == 1) 26  { 27 cout<<"YES"<<endl; 28  } 29 else 30  { 31 cout<<"NO"<<endl; 32  } 33 return 0; 34 }
复制代码

 

 

2.你有n个字符串。 每个字符串由小写英文字母组成。 重新排序给定的字符串,使得对于每个字符串,在它之前的所有字符串都是它的子串。

https://www.cnblogs.com/wkfvawl/p/9229758.html

复制代码
 1 #include<string>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<iostream>
 5 using namespace std;  6 bool cmp(string a, string b)  7 {  8 if (a.length() == b.length())  9 return a < b; 10 return a.length() < b.length(); 11 } 12 int main() 13 { 14 int n; 15 string s[111]; 16 scanf("%d", &n); 17 for (int i = 0; i < n; i++) 18  { 19 cin >> s[i]; 20  } 21 sort(s, s + n, cmp); 22 int flag = 1; 23 for (int i = 1; i < n; i++) 24  { 25 if (s[i].find(s[i-1]) == string::npos) 26  { 27 flag = 0; 28 break; 29  } 30  } 31 if (flag) 32  { 33 cout << "YES" << endl; 34 for (int i = 0; i < n; i++) 35  { 36 cout << s[i] << endl; 37  } 38  } 39 else 40  { 41 cout << "NO" << endl; 42  } 43 return 0; 44 }
复制代码

转载于:https://www.cnblogs.com/bhd123/p/9453662.html

### C++ STL 中 `find` 函数用法C++ 标准模板库 (STL) 中,`std::find` 是一种用于在一范围内查找特定值的算法。此函数定义于头文件 `<algorithm>` 中。 #### 基本语法 ```cpp #include <algorithm> InputIt find(InputIt first, InputIt last, const T& value); ``` 该函数接受三个参数:两个迭代器分别指向要搜索范围的第一个位置和最后一个位置之后的位置,以及一个待寻找的目标值。如果找到目标,则返回指向第一个匹配项的迭代器;如果没有找到任何匹配项,则返回 `last` 迭代器[^2]。 #### 使用示例 下面是一个简单的例子来演示如何使用 `std::find` 查找向量中的整数: ```cpp #include <iostream> #include <vector> #include <algorithm> // For std::find int main() { std::vector<int> numbers = {10, 20, 30, 40, 50}; auto it = std::find(numbers.begin(), numbers.end(), 30); if(it != numbers.end()){ std::cout << "Element found at position: " << std::distance(numbers.begin(),it) << '\n'; }else{ std::cout << "Element not found\n"; } return 0; } ``` 这段代码会输出:“Element found at position: 2”,因为数值 30 存在于索引 2 处(基于零计数)。 如果未发现指定元素,则消息 “Element not found” 将被打印出来[^3]。 #### 注意事项 当处理自定义对象时,确保这些类具有合适的比较操作符重载以便能够正确执行相等性测试。对于字符串类型的转换问题可以参照之前提到的方法将 `QString` 转换成标准 C++ 字符串再做对比[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值