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
随机推荐
- nginx 日志分析工具goaccess
参考:https://www.goaccess.io/download 安装 $ wget http://tar.goaccess.io/goaccess-1.1.1.tar.gz $ tar -xz ...
- Android简易实战教程--第四十七话《使用OKhttp回调方式获取网络信息》
在之前的小案例中写过一篇使用HttpUrlConnection获取网络数据的例子.在OKhttp盛行的时代,当然要学会怎么使用它,本篇就对其基本使用做一个介绍,然后再使用它的接口回调的方式获取相同的数 ...
- arm-none-eabi-g++ -Xlinker -T "../LF3Kmonitor.ld" -Xlinker -Map="Bogota_ICT_V.map"-ram-hosted.ld -mc
1.arm-none-eabi-g++:是编译ARM裸板用的编译器,不依赖于操作系统. 2.-Xlinker -T "../LF3Kmonitor.ld" -Xlinker -Ma ...
- 物料分类新增&更新
--新增 INV_ITEM_CATEGORY_PUB.Create_Category ( p_api_version IN NUMBER, p_init_msg_list IN VARCHAR2 DE ...
- activiti 数据库连接配置
1.1.1. 前言 在activiti 动态配置 activiti 监听引擎启动和初始化(高级源码篇)一文中,我们讲解了如何动态的配置DataSource 当我们程序配置了DataSource,act ...
- x264源代码简单分析:概述
===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...
- 22 Notification样式设置内部按钮点击事件
package com.exam1ple.demo1; import android.app.Activity; import android.app.NotificationManager; imp ...
- XMPP系列(七)---获取群组列表
上一篇介绍了如何创建群组,这一篇就介绍一下,如何获取自己的群组列表. 在上一篇有提到,如果我们创建的群组是公共的群组,那么获取自己的群组列表时,会获取到自己的群组列表和那些公共的群组.而实际做社交的应 ...
- 闪屏页面开发遇到的问题you need to use a theme.appcompat theme (or descendant)
开始做一个新闻客户端的应用,在做到闪屏页面时之前发布应用的时候总是报错,原因是我在splash.java中把Activty写成ActionBarActivity,导包,然后就可以了.以前也遇到过这种情 ...
- linux C++多线程操作文件&输出加锁
下述demo将指定目录下文件存入vector,然后一分为二交给两个线程(threadTask1,threadTask2)去分别处理,对输出函数printDirent加锁防止紊乱. #include & ...