STL 之find,find_if,find_end,find_first_of
来源:互联网 发布:银行网络宣传方法 编辑:程序博客网 时间:2024/05/18 17:58
作用:用来在一个指定的区间中查找元素。
1,find, find_if
原型:
- #include <algorithm>
- template <class inputItr,class size,class Type>
- inputItr find(inputItr first, inputItr last, const Type& searchValue);
- template <class inputItr, class unaryPredicate>
- inputItr find_if(inputItr first, inputItr last, UnaryPredicate op);
示例代码:
- #include <iostream>
- #include <list>
- #include <string>
- #include <numeric>
- #include <iterator>
- #include <vector>
- #include <functional>
- #include <algorithm>
- using namespace std;
- int main() {
- char cList[10] = {'a','i','C','d','e','f','o','H','u','j'};
- vector<char> charList(cList,cList+10);
- ostream_iterator<char> screen(cout, " ");
- cout << "charList:" << endl;
- copy(charList.begin(),charList.end(),screen);
- cout << endl;
- // 定义迭代器
- vector<char>::iterator position;
- position = find(charList.begin(),charList.end(),'d');
- if ( position != charList.end())
- {
- cout << "position = " << (position - charList.begin()) << endl;
- } else {
- cout << "the element is not in the list" << endl;
- }
- position = find_if(charList.begin(),charList.end(),isupper);
- if ( position != charList.end())
- {
- cout << "position = " << (position - charList.begin()) << endl;
- } else {
- cout << "the element is not in the list" << endl;
- }
- return 0;
- }
运行结果:
charList:
a i C d e f o H u j
position = 3
position = 2
2,find_end,find_first_of
声明:
- #include <algorithm>
- template <class forwardItr1,class forwardItr2>
- forwardItr1 find_end(forwardItr1 first1, forwardItr1 last1,forwardItr2 first2,forwardItr2 last2);
- template <class forwardItr1,class forwardItr2,class binaryPredicate>
- forwardItr1 find_end(forwardItr1 first1, forwardItr1 last1,forwardItr2 first2,forwardItr2 last2,binaryPredicate op);
- template <class forwardItr1,class forwardItr2>
- forwardItr1 find_first_of(forwardItr1 first1, forwardItr1 last1,forwardItr2 first2,forwardItr2 last2);
- template <class forwardItr1,class forwardItr2,class binaryPredicate>
- forwardItr1 find_first_of(forwardItr1 first1, forwardItr1 last1,forwardItr2 first2,forwardItr2 last2,binaryPredicate op);
示例代码:
- #include <iostream>
- #include <list>
- #include <string>
- #include <numeric>
- #include <iterator>
- #include <vector>
- #include <functional>
- #include <algorithm>
- using namespace std;
- int main() {
- int list1[10] = {12,34,56,21,34,78,34,56,12,25};
- int list2[2] = {34,56};
- int list3[3] = {56,21,35};
- int list4[5] = {33,48,21,34,73};
- vector<int> vecList1(list1,list1+10);
- vector<int> vecList2(list2,list2+2);
- vector<int> vecList3(list3,list3+3);
- vector<int> vecList4(list4,list4+5);
- vector<int>::iterator localtion;
- ostream_iterator<int> screen(cout, " ");
- cout << "List1" << endl;
- copy(list1,list1+10,screen);
- cout << endl;
- cout << "List2" << endl;
- copy(list2,list2+2,screen);
- cout << endl;
- // find_end 查找最后一个匹配
- // 在vecList1 中查找vecList2
- localtion = find_end(vecList1.begin(),vecList1.end(),vecList2.begin(),vecList2.end());
- if (localtion != vecList1.end())
- {
- cout << "position = " << (localtion - vecList1.begin()) << endl;
- } else {
- cout << "vecList2 is not in list1" << endl;
- }
- cout << "List3:" << endl;
- copy(vecList3.begin(),vecList3.end(),screen);
- cout << endl;
- localtion = find_end(vecList1.begin(),vecList1.end(),vecList3.begin(),vecList3.end());
- if (localtion != vecList1.end())
- {
- cout << "position = " << (localtion - vecList1.begin()) << endl;
- } else {
- cout << "vecList3 is not in list1" << endl;
- }
- cout << "List4:" << endl;
- copy(vecList4.begin(),vecList4.end(),screen);
- cout << endl;
- // find_first_of 首次出现的位置
- // 可以检查两个容器中元素是否相互包含
- localtion = find_first_of(vecList1.begin(),vecList1.end(),vecList4.begin(),vecList4.end());
- if (localtion != vecList1.end())
- {
- cout << "position = " << (localtion - vecList1.begin()) << endl;
- } else {
- cout << "No element of List4 is in list1" << endl;
- }
- return 0;
- }
运行结果:
List1
12 34 56 21 34 78 34 56 12 25
List2
34 56
position = 6
List3:
56 21 35
vecList3 is not in list1
List4:
33 48 21 34 73
position = 1
0 0
- STL 之find,find_if,find_end,find_first_of
- STL 之find,find_if,find_end,find_first_of
- 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)
- 非变异算法之查询( find(), find_if(), find_first_of, adjacent_find(), find_end(), search(), search_n() )
- find()、find_end()、find_first_of()、find_if()的使用示例和区别
- find,find_if,find_end函数
- STL 之 find find_if
- STL 之find & find_if
- find_end,find_first_of
- C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用
- C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用
- C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用
- C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用
- C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用
- find ,find_first_of , find_if , adjacent_find的使用
- STL之for_each, cout, cout_if, find, find_if
- stl find和find_if
- STL-find,find_if函数
- 题目1518:反转链表
- 2011斯坦福大学iOS应用开发教程学习笔记(第六课)故事版
- Pyc文件 编译和运行
- VC 无法查找或打开 PDB 文件
- ArcSDE 图层创建、注册、插入数据与删除数据
- STL 之find,find_if,find_end,find_first_of
- 几种常见无线摄像头的传输距离比较
- STL 之remove,remove_if,remove_copy,remove_copy_if
- 动力火车
- 2011斯坦福大学iOS应用开发教程学习笔记(第八课)viewController生命周期
- C++ 窗口 设计尝试
- Oracle学习笔记(14)----------用户管理
- linux-shell syntax error: unexpected end of file
- 第2章 21