STL_算法_查找算法(find、find_if)
2017-07-13 20:49
555 查看
C++ Primer 学习中。
。。
简单记录下我的学习过程 (代码为主)
find 、 find_if
/**********************线性查找O(n) find(); find_if(); 注意: 1.假设是已序区间,能够使用区间查找算法 2.关联式容器(set,map)有等效的成员函数find();时间复杂度O(log(n)) 3.string 有等效的成员函数find(); **********************/ #include<iostream> #include<cstdio> #include<string> #include<vector> #include<set> #include<algorithm> #include<functional> using namespace std; /************************************************************************************* std::find algorithm -------------------------------------------------------------------------------------- template <class InputIterator, class T> InputIterator find ( InputIterator first, InputIterator last, const T& value ); eg: template<class InputIterator, class T> InputIterator find ( InputIterator first, InputIterator last, const T& value ) { for ( ;first!=last; first++) if ( *first==value ) break; return first; } **************************************************************************************/ /************************************************************************************* std::find_if algorithm -------------------------------------------------------------------------------------- template <class InputIterator, class Predicate> InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred ); eg: template<class InputIterator, class Predicate> InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred ) { for ( ; first!=last ; first++ ) if ( pred(*first) ) break; return first; } **************************************************************************************/ bool IsEven (int i); int main () { int myints[] = {10,30,20,40,20,10,30,40}; int * p; // pointer to array element: p = find(myints,myints+8,30); ++p; cout << "The element following 30 is " << *p << endl; vector<int> myvector (myints,myints+8); vector<int>::iterator it; // iterator to vector element: it = find (myvector.begin(), myvector.end(), 30); ++it; cout << "The element following 30 is " << *it << endl; //输出第一个30---第二个30区间内的数 vector<int>::iterator it2; it2=find (it,myvector.end(),30); while(it!=it2) cout<<*it++<<" "; cout<<endl; /**--------------------------------find_if()---------------------------------**/ //找第一个偶数 it = find_if (myvector.begin(), myvector.end(), IsEven);//函数 或 函数对象 cout << "The first odd value is " << *it << endl; it2 = find_if(myvector.begin(),myvector.end(), not1(bind2nd(modulus<int>(),2))); cout << "The first odd value is " << *it2 << endl; /**--------------------------------关联容器---------------------------------**/ set<int> s(myvector.begin(),myvector.begin()+4); cout<<"复杂度为O(log(n)),查找*s.find(40):= " << *s.find(40) << endl; /**---------------------------------string----------------------------------**/ string st("AngelaBaby"); string::size_type pos = st.find("Baby"); if(pos != string::npos) cout<<"find it!"<<endl; else cout<<"not find it!"<<endl; pos = st.find("baby"); if(pos != string::npos) cout<<"find it!"<<endl; else cout<<"not find it!"<<endl; return 0; } bool IsEven (int i) { return ((i%2)==0); } /****** Output: The element following 30 is 20 The element following 30 is 20 20 40 20 10 The first odd value is 10 The first odd value is 10 复杂度为O(log(n)),查找*s.find(40):= 40 find it! not find it! ******/
相关文章推荐
- STL_算法_查找算法(find、find_if)
- 使用STL通用算法find_if()在list中搜索对象
- STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation
- C++ STL 算法:查找算法(1)find、find_if
- STL源码剖析——STL算法之find查找算法
- c++之STL(13) STL 算法 - 查找算法(4)find_first_of(b,e,sb,se) find_first_of(b,e,sb,se,bp)
- Windows STL算法学习一 find与find_if
- STL算法find_if和find
- STL 算法Find_if_not+匿名函数
- STL 查找vector容器中的指定对象:find()与find_if()算法
- C++ STL 算法:查找算法(4)find_first_of
- STL_算法_查找算法(find_first_of、find_last_of)
- STL之vector,数组线性容器array,list容器,算法find,find_if,bind1st,仿函数
- C++ STL 算法:查找算法(5) vadjacent_find
- STL_算法_查找算法(adjacent_find)
- c++之STL(13) STL 算法 - 查找算法(3)search() find_end()
- stl 的find_if算法用来查找容器内的符合条件的元素
- STL 查找vector容器中的指定对象:find()与find_if()算法
- c++之STL(13) STL 算法 - 查找算法(5)adjacent_find(b,e) adjacent_find(b,e,p)
- STL源码剖析——STL算法之find查找算法