常用STL算法2_查找
阅读量:4049 次
发布时间:2019-05-25
本文共 3704 字,大约阅读时间需要 12 分钟。
#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include #include #include #include //输出流using namespace std;//谓词1bool FindEquelNum1(int &num1, int &num2){ return (num1 == num2)?true:false;}//谓词2struct FindEquelNum2{public: FindEquelNum2(int n) { m_num = n; } bool operator()(int &num1, int &num2) { if ((num1 == num2) && (num1 == m_num)) return true; else return false; }private: int m_num;};void main071_adjacent_find(){ vector vecInt; vecInt.push_back(1); vecInt.push_back(2); vecInt.push_back(2); vecInt.push_back(4); vecInt.push_back(5); vecInt.push_back(5); //1. adjacent_find: find first matching successor //vector ::iterator it = adjacent_find(vecInt.begin(), vecInt.end());//默认找第一个 ok //vector ::iterator it = adjacent_find(vecInt.begin(), vecInt.end(),FindEquelNum1);谓词1 ok //2. find first satisfying _Pred with successor vector ::iterator it = adjacent_find(vecInt.begin(), vecInt.end(), FindEquelNum2(5));//找到满足条件的第一个//谓词2 ok if (it == vecInt.end()) { cout<<"not find"<
setInt; setInt.insert(3); setInt.insert(1); setInt.insert(7); setInt.insert(5); setInt.insert(9); /* bool binary_search(_FwdIt _First, _FwdIt _Last, const _Ty& _Val) { // test if _Val equivalent to some element, using operator< _First = _STD lower_bound(_First, _Last, _Val); return (_First != _Last && !(_Val < *_First)); } */ bool bFind = binary_search(setInt.begin(),setInt.end(),5); if (bFind) { cout<<"binary_search succ"<
vecInt; vecInt.push_back(1); vecInt.push_back(2); vecInt.push_back(2); vecInt.push_back(4); vecInt.push_back(5); vecInt.push_back(5); /* count(_InIt _First, _InIt _Last, const _Ty& _Val) { // count elements that match _Val _DEBUG_RANGE(_First, _Last); return (_Count_np(_Unchecked(_First), _Unchecked(_Last), _Val)); } typename iterator_traits<_InIt>::difference_type _Count_np(_InIt _First, _InIt _Last, const _Ty& _Val) { // count elements that match _Val typename iterator_traits<_InIt>::difference_type _Count = 0; for (; _First != _Last; ++_First) if (*_First == _Val) ++_Count; return (_Count); } */ int num = count(vecInt.begin(), vecInt.end(), 5); cout<<"num:"<
< 3);}void main074_count_if(){ vector vecInt; vecInt.push_back(1); vecInt.push_back(2); vecInt.push_back(2); vecInt.push_back(4); vecInt.push_back(5); vecInt.push_back(5); /* typename iterator_traits<_InIt>::difference_type count_if(_InIt _First, _InIt _Last, _Pr _Pred) { // count elements satisfying _Pred _DEBUG_RANGE(_First, _Last); _DEBUG_POINTER(_Pred); return (_Count_if(_Unchecked(_First), _Unchecked(_Last), _Pred)); } */ int num = count_if(vecInt.begin(), vecInt.end(), GreatNum); cout<<"num:"< < vecInt; vecInt.push_back(1); vecInt.push_back(3); vecInt.push_back(5); vecInt.push_back(7); vecInt.push_back(9); /* _InIt find(_InIt _First, _InIt _Last, const _Ty& _Val) { // find first matching _Val _DEBUG_RANGE(_First, _Last); return (_Rechecked(_First, _Find(_Unchecked(_First), _Unchecked(_Last), _Val))); } */ vector
::iterator it = find(vecInt.begin(), vecInt.end(), 5); cout<<*it<
vecInt; vecInt.push_back(1); vecInt.push_back(3); vecInt.push_back(5); vecInt.push_back(7); vecInt.push_back(9); /* _InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred) { // find first satisfying _Pred _DEBUG_RANGE(_First, _Last); _DEBUG_POINTER(_Pred); return (_Rechecked(_First, _Find_if(_Unchecked(_First), _Unchecked(_Last), _Pred))); } */ vector