博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
常用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
::iterator it = find_if(vecInt.begin(), vecInt.end(), GreatNum); cout<<*it<
::iterator it2 = vecInt.begin(); int total_cnt = 0; int dis2 = 0; for (;it2 != vecInt.end(); it2++) { it2 = find_if(it2, vecInt.end(), GreatNum); dis = distance(vecInt.begin(), it2); cout<<"location:"<
<<" elem:"<<*it2<

转载地址:http://xpnci.baihongyu.com/

你可能感兴趣的文章
Qt /INCLUDE:?warp_size@cuda@at@@YAHXZ
查看>>
Faster-RCNN网络详解
查看>>
Litorch+VS2017+Qt环境配置教程
查看>>
yolo训练精简版
查看>>
基于GB28181RTPoverTCP的发送程序拾遗
查看>>
Android入门知识要点
查看>>
libcurl异步请求+http长连接池
查看>>
2440机器码
查看>>
c语言内存分配
查看>>
结构体file_operations
查看>>
TFT LCD
查看>>
双向链表
查看>>
二级指针与指针数组的关系
查看>>
Linux系统限制
查看>>
C预处理器标识
查看>>
static用法总结
查看>>
const用法小结
查看>>
malloc、free与内存碎片
查看>>
C语言实现库函数
查看>>
Tarball的管理
查看>>