stl常用的查找算法
2024-10-21 02:41:51
#include<iostream>
using namespace std;
#include"vector"
#include"algorithm"
#include"functional" bool lowThree(int &iNum)
{
return (iNum > 3);
}
int main()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(2);
v1.push_back(6);
v1.push_back(8); // adjacent_find在iterator对标识元素范围内,查找一对相邻重复元素,
//找到则返回指向这对元素的第一个元素的迭代器。否则返回past-the-end。
vector<int> ::iterator it = adjacent_find(v1.begin(), v1.end());
cout << *it << endl;
//初步结论,在stl算法中的谓词不能用数字代替,而函数可以用数字代替 //在有序序列中查找value,找到则返回true。注意:在无序序列中,不可使用。
bool b1 = binary_search(v1.begin(), v1.end(), 6);
cout << b1 << endl; //利用等于操作符,把标志范围内的元素与输入值比较,返回相等的个数。
int i1 = count(v1.begin(), v1.end(), 5);
cout << i1 << endl;
//参数中要求是谓词。可以直接写一个回调函数
int i2 = count_if(v1.begin(), v1.end(), lowThree);
cout << i2 << endl; vector<int> ::iterator it22 = find(v1.begin(), v1.end(), 5);
cout << *it22 << endl; vector<int> ::iterator it3 = find_if(v1.begin(), v1.end(), lowThree);
cout << *it3 << endl; system("pause");
}
最新文章
- poj 3734 Blocks
- ios 写项目的时候遇到的问题及解决方案(3)
- 利用SQL 建立和删除 LINKED SERVER
- Oracle双实例切换
- Composite模式
- [Ubuntu] ubuntu13.04 从php5.4降级到php5.3
- spark中streamingContext的使用详解
- ABBYY FineReader 12PDF选项卡有保存模式吗
- 利用反射自动生成SQL语句(仿Linq)
- 【Tools】Apache Maven 入门篇 ( 上 )
- 通过源码安装最新版Git
- iOS 苹果app提交 ITC.apps.validation.prerelease_build_missing
- delphi 实现微信开发
- [LeetCode]题解(python):141-Linked List Cycle
- ORA-12545: Connect failed because target host or object does not exist
- FlexPaper+SWFTool+操作类=在线预览PDF(转)
- HDU 5778 abs
- [译]前端JS面试题汇总 Part 1(事件委托/this关键字/原型链/AMD与CommonJS/自执行函数)
- 安装VisualStudio时失败,错误信息安装包失败或证书不在有效期内
- linux 设备驱动概述