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
随机推荐
- Bootstrap3 排版-内联文本元素
标记文本 突出显示的文本由于其相关性在另一个上下文中,使用<mark>标记. You can use the mark tag to highlight text. You can use ...
- logistic分类
对Logistic回归模型,个人做的一些总结: 公式就不套用了,教材上面基本都有而且详细.logistic回归用图形化形式描述如下: logistic回归是一种简单高效的分类模型,它不仅可以通过学习来 ...
- 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 ...
- ROS_Kinetic_28 turtlebot gazebo demo例子
ROS_Kinetic_28 turtlebot gazebo demo例子 官方教程:http://wiki.ros.org/turtlebot_gazebo/Tutorials/indigo/Ma ...
- Android逆向工程
在Root前提下,我们可以使用Hooker方式绑定so库,通过逆向方式篡改数值,从而达到所谓破解目的.然而,目前无论是软件加固方式,或是数据处理能力后台化,还是客户端数据真实性验证,都有了一定积累和发 ...
- 21 ViewPager RadioGroup
结构 MainActivity.java package com.qf.day21_viewpagerfragmentrg_demo4; import java.util.ArrayList; imp ...
- Struts 2 之文件上传
如果要获得上传文件的原始名称,需要定义一个String类型的属性,属性名必须为***FileName,其中***为File属性的名称:同理,如果要获取该文件的MIME类型,需要定义一个***Conte ...
- JSP1.x 自定义标签
Tag接口 任何一个标签都对应着一个java类,该类必须实现Tag接口,JSP遇到一个标签后后,将通过一个tld文件查找该标签的实现类,并运行该类的相关方法 import javax.servlet. ...
- 学习Tensorflow,反卷积
在深度学习网络结构中,各个层的类别可以分为这几种:卷积层,全连接层,relu层,pool层和反卷积层等.目前,在像素级估计和端对端学习问题中,全卷积网络展现了他的优势,里面有个很重要的层,将卷积后的f ...
- 【iOS 开发】Objective - C 面向对象 - 方法 | 成员变量 | 隐藏封装 | KVC | KVO | 初始化 | 多态
一. Objective-C 方法详解 1. 方法属性 (1) OC 方法传参机制 Object-C 方法传参机制 : OC 中得参数传递都是值传递, 传入参数的是参数的副本; -- 基本类型 (值传 ...