cb31a_c++_STL_算法_查找算法_(4)find_first_of
cb31a_c++_STL_算法_查找算法_(4)find_first_of
find_first_of(b,e,sb,se),sb,second begin, se,second end();
find_first_of(b,e,sb,se,bp),bp--谓词,就是一个函数,或者函数对象,返回一个bool
使用逆向迭代器,实现string类的rfind.找最后一个。reverse_find=rfind
没有find_last_of算法,string类的成员函数有find_last_of
find()
find_if()
search_in()
serarch()
find_end()
find_first_of()
adjacent_find()
txwtech@163.com
1 /*cb31a_c++_STL_算法_查找算法_(4)find_first_of 2 find_first_of(b,e,sb,se),sb,second begin, se,second end(); 3 find_first_of(b,e,sb,se,bp),bp--谓词,就是一个函数,或者函数对象,返回一个bool 4 5 使用逆向迭代器,实现string类的rfind.找最后一个。reverse_find=rfind 6 没有find_last_of算法,string类的成员函数有find_last_of 7 8 find() 9 find_if() 10 search_in() 11 serarch() 12 find_end() 13 find_first_of() 14 adjacent_find() 15 txwtech@163.com 16 */ 17 18 #include <iostream> 19 #include <vector> 20 #include <list> 21 #include <algorithm> 22 #include <string> 23 24 using namespace std; 25 26 int main() 27 { 28 vector<int> ivec; 29 list<int> searchList; 30 for (int i = 1; i <= 11; ++i) 31 { 32 ivec.push_back(i); 33 } 34 for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter) 35 cout << *iter << ' '; 36 cout << endl; 37 searchList.push_back(3); 38 searchList.push_back(6); 39 searchList.push_back(9); 40 41 cout << "ivec里面找,3,6,9" << endl; 42 vector<int>::iterator pos; 43 pos=find_first_of(ivec.begin(), ivec.end(), searchList.begin(), searchList.end()); 44 if (pos != ivec.end()) 45 cout << "找到了,位置是:" << distance(ivec.begin(), pos) + 1 << endl; 46 else 47 cout << "没找到" << endl; 48 49 vector<int>::reverse_iterator rpos; 50 // rpos.base(),转换成正向迭代器,就不需要加1了 51 rpos= find_first_of(ivec.rbegin(), ivec.rend(), searchList.begin(), searchList.end()); 52 if (rpos != ivec.rend()) 53 cout << "逆向迭代器找到了,位置是:" << distance(ivec.begin(), rpos.base()) << endl; 54 else 55 cout << "没找到" << endl; 56 57 string numerics("0123456789"); 58 string name("ra82d3k"); 59 60 cout << "name里面找numerics的内容" << endl; 61 string::size_type posn= name.find_first_of(numerics);//顺序查找 62 if (posn != string::npos) 63 cout << "posn不等于npos,npos表示结束位置,已经扎到了,下标:" << posn << endl; 64 else 65 cout << "没找到" << endl; 66 67 posn = name.find_last_of(numerics);//逆向查找 68 if (posn != string::npos) 69 cout << "逆向查找,最后一位开始找,已经扎到了,下标:" << posn << endl; 70 else 71 cout << "没找到" << endl; 72 73 74 return 0; 75 }
欢迎讨论,相互学习。
cdtxw@foxmail.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)