STL常用查找算法介绍
adjacent_find()
在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器。否则返回past-the-end。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
void play_adjacent_find()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(2);
v1.push_back(3);
v1.push_back(5);
vector<int>::iterator it = adjacent_find(v1.begin(), v1.end());
if (it == v1.end())
{
cout << "没有找到 重复的元素" << endl;
}
else
{
cout << *it << endl;
}
// 2
int index = distance(v1.begin(), it);
cout << index << endl;
// 1
}
int main()
{
play_adjacent_find();
return 0;
}
binary_search()
在有序序列中查找value,找到则返回true。注意:在无序序列中,不可使用。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
// binary_search是对排序好的进行查找
void play_binary_search()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
v1.push_back(7);
v1.push_back(9);
bool b = binary_search(v1.begin(), v1.end(), 7);
if (b) {
cout << "find success\n";
}
else {
cout << "find fail\n";
}
// find success
}
int main()
{
play_binary_search();
return 0;
}
count()
利用等于操作符,把标志范围内的元素与输入值比较,返回相等的个数。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
void play_count()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(3);
v1.push_back(7);
v1.push_back(9);
v1.push_back(3);
int cnt = count(v1.begin(), v1.end(), 3);
cout << "count of 3: " << cnt << endl;
// count of 3: 3
}
int main()
{
play_count();
return 0;
}
count_if()
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
bool GreaterThree(const int &a)
{
return a > 3;
}
void play_count_if()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(4);
v1.push_back(7);
v1.push_back(9);
v1.push_back(3);
int cnt = count_if(v1.begin(), v1.end(), GreaterThree);
cout << "count of greater 3: " << cnt << endl;
// count of greater 3: 3
}
int main()
{
play_count_if();
return 0;
}
find()
find: 利用底层元素的等于操作符,对指定范围内的元素与输入值进行比较。当匹配时,结束搜索,返回该元素的迭代器。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
void play_find()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(4);
v1.push_back(7);
v1.push_back(9);
v1.push_back(3);
vector<int>::iterator it = find(v1.begin(), v1.end(), 4);
if (it == v1.end()) {
cout << "find fail\n";
}
else {
cout << "find success\n";
}
// find success
}
int main()
{
play_find();
return 0;
}
find_if()
find_if: 使用输入的函数代替等于操作符执行find。返回被找到的元素的迭代器。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
bool GreaterThree(const int &a)
{
return a > 3;
}
void play_find_if()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(4);
v1.push_back(7);
v1.push_back(9);
v1.push_back(3);
vector<int>::iterator it = find_if(v1.begin(), v1.end(), GreaterThree);
if (it == v1.end()) {
cout << "find fail\n";
}
else {
cout << "find success\n";
cout << "value: " << *it << endl;
}
// find success
// value: 4
}
int main()
{
play_find_if();
return 0;
}
STL常用查找算法介绍的更多相关文章
- C++ STL 常用查找算法
C++ STL 常用查找算法 adjacent_find() 在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器.否则返回past-the-end. ...
- STL常用排序算法介绍
merge() 以下是排序和通用算法:提供元素排序策略 merge: 合并两个有序序列,存放到另一个序列. #include <iostream> #include <cstdi ...
- 常用查找算法(Java)
常用查找算法(Java) 2018-01-22 1 顺序查找 就是一个一个依次查找 2 二分查找 二分查找(Binary Search)也叫作折半查找. 二分查找有两个要求, 一个是数列有序, 另一个 ...
- C++ STL 常用排序算法
C++ STL 常用排序算法 merge() 以下是排序和通用算法:提供元素排序策略 merge: 合并两个有序序列,存放到另一个序列. 例如: vecIntA,vecIntB,vecIntC是用ve ...
- C++ STL 常用遍历算法
C++ STL 常用遍历算法 STL的容器算法迭代器的设计理念 1) STL的容器通过类模板技术,实现数据类型和容器模型的分离 2) STL的迭代器技术实现了遍历容器的统一方法:也为STL的算法提供了 ...
- C++ STL之查找算法
C++STL有好几种查找算法,但是他们的用法上有很多共同的地方: 1.除了binary_search的返回值是bool之外(查找的了返回true,否则返回false),其他所有的查找算法返回值都是一个 ...
- STL常用遍历算法for_each和transform的比较
for_each()和transform()算法比较 1)STL 算法 – 修改性算法 for_each() copy() copy_backward() transform() merge ...
- C语言实现常用查找算法——二分查找
#include<stdio.h> void insert_sort(int a[],int n); int binary_search(int a[],int x,int n); voi ...
- python实现常用查找算法
http://www.cnblogs.com/feixuelove1009/p/6148357.html
随机推荐
- 豌豆夹Redis解决方案Codis源码剖析:Proxy代理
豌豆夹Redis解决方案Codis源码剖析:Proxy代理 1.预备知识 1.1 Codis Codis就不详细说了,摘抄一下GitHub上的一些项目描述: Codis is a proxy base ...
- Android系统对话框
Android系统对话框 效果图 2个按钮的对话框 3个按钮的对话框 自定义View的对话框 单选对话框 多选对话框 列表框 Code XML <?xml version="1.0&q ...
- Android开发之手把手教你写ButterKnife框架(一)
欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52662376 本文出自:[余志强的博客] 一.概述 JakeWhar ...
- MongoDB实用教程
---------------------------------------------------------------------------------------------------- ...
- ssh用法及命令
http://blog.csdn.net/pipisorry/article/details/52269785 什么是SSH? 简单说,SSH是一种网络协议,用于计算机之间的加密登录.如果一个用户从本 ...
- Scikit-learn:模型选择Model selection
http://blog.csdn.net/pipisorry/article/details/52250983 选择合适的estimator 通常机器学习最难的一部分是选择合适的estimator,不 ...
- ViewPager 几个状态详解
ViewPager.SCROLL_STATE_DRAGGING 当用户按下ViewPager视图并且需要滑动第一下时; ViewPager.SCROLL_STATE_SETTLING: 当用户滑动的放 ...
- windows平台下 c/c++进行http通信的教训
由于需要使用c++开发一个桌面应用软件,需要用到http请求进行通讯,也是本人第一次进行网络相关的开发工作,遇到了不少坑. 由于是在windows下开发和使用的应用软件,自然而然想到了调用Window ...
- JDBC编程-事务编程(四)
事务的概念 事务的概念在我看来是指的是一组sql序列,这个序列是一块执行的单位,要么全部执行,要不全部执行,这样可以很好的对数据库进行并发控制. 因为数据库是多个用户都可以同时操作的,如果多个用户同时 ...
- 使用git-flow来帮助管理git代码
对git不熟悉的我,经常把git提交搞得很乱,导致在master上有许多无用的commit,最终决定好好地看一下git的使用教程,却不小心发现了还有一个git-flow的工具可以帮助我管理好git项目 ...