STL 之search,search_n,sort,binary_search
作用:用来查找元素和元素排序
声明:
- #include <algorithm>
- template <class forwardItr1, class forwardItr2>
- forwardItr1 search(forwardItr1 first1, forwardItr1 last1,forwardItr2 first2,forwardItr2 last2);
- template <class forwardItr1, class forwardItr2,class binaryPredicate>
- forwardItr1 search(forwardItr1 first1, forwardItr1 last1,forwardItr2 first2,forwardItr2 last2,binaryPredicate op);
- template <class forwardItr, class size,class Type>
- forwardItr search_n(forwardItr first, forwardItr last,size count,const Type& value);
- template <class forwardItr, class size,class Type,class binaryPredicate>
- forwardItr search_n(forwardItr first, forwardItr last,size count,const Type& value,binaryPredicate op);
- template<class randomAccessItr>
- void sort(randomAccessItr first,randomAccessItr last);
- template<class randomAccessItr, class compare>
- void sort(randomAccessItr first, randomAccessItr last, compare op);
- template<class forwardItr,class Type>
- bool binary_search(forwardItr first,forwardItr last,const Type& searchValue);
- template<class forwardItr,class Type,class compare>
- bool binary_search(forwardItr first, forwardItr last, const Type& searchValue,compare op);
示例代码:
- #include <iostream>
- #include <list>
- #include <string>
- #include <numeric>
- #include <iterator>
- #include <vector>
- #include <functional>
- #include <algorithm>
- using namespace std;
- int main() {
- int intList[15] = {12,34,56,34,34,
- 78,38,43,12,25,
- 34,56,62,5,49};
- vector<int> vecList(intList,intList+15);
- int list[2] = {34, 56};
- vector<int>::iterator location;
- ostream_iterator<int> screen(cout, " ");
- cout << "vecList:" << endl;
- copy(vecList.begin(),vecList.end(),screen);
- cout << endl;
- cout << "list:" << endl;
- copy(list,list+2,screen);
- cout << endl;
- // search:查找一个集合是否在另一集合中
- // 与find的区别是,find是查找某一个元素
- location = search(vecList.begin(),vecList.end(),list,list + 2);
- if (location != vecList.end())
- {
- cout << "location:" << (location - vecList.begin()) << endl;
- } else {
- cout << "list is not in vecList" << endl;
- }
- // search_n :查找某个元素第n此出现的位置
- location = search_n(vecList.begin(),vecList.end(),2,34);
- if (location != vecList.end())
- {
- cout << "location:" << (location - vecList.begin()) << endl;
- } else {
- cout << "list is not in vecList" << endl;
- }
- // sort
- sort(vecList.begin(),vecList.end());
- cout << "vecList:" << endl;
- copy(vecList.begin(),vecList.end(),screen);
- cout << endl;
- bool found;
- // 用二分法查找:前提先排序
- found = binary_search(vecList.begin(),vecList.end(),43);
- if (found)
- {
- cout << "43 found in vecList." << endl;
- } else {
- cout << "43 not found in vecList." << endl;
- }
- return 0;
- }
运行结果:
vecList:
12 34 56 34 34 78 38 43 12 25 34 56 62 5 49
list:
34 56
location:1
location:3
vecList:
5 12 12 25 34 34 34 34 38 43 49 56 56 62 78
43 found in vecList.
STL 之search,search_n,sort,binary_search相关推荐
- C++的STL库,vector sort排序时间复杂度 及常见容器比较
http://www.cnblogs.com/sthv/p/5511921.html http://www.169it.com/article/3215620760.html http://www.c ...
- c语言的erase函数,C++ STL标准库中sort,erase,remove_if函数的使用
本篇介绍C++ STL中常用的几个函数 1.std::sort 1.1.sort函数包含在头文件为#include的c++标准库中,调用标准库里的排序方法可以实现对数据的排序. 1.2.sort函数的 ...
- C++ : STL常用算法: inner_product , sort ,itoa
目录 1.std::count 2.std::inner_product 3.atoi 4.itoa 5 is_sorted 6 sort 7. fill 8 mismatch 1.std::cou ...
- c++ STL find search
#include <iostream> #include <algorithm> #include <deque> #include <list> #i ...
- C++STL算法 search你是我的半截的诗
wchar_t这种宽字符型变量在打印的时候不能像下面那样直接打出来, copy(wvec.begin(), wvec.end(), ostream_iterator<int>(cout, ...
- C++STL算法 search 在圆周率中寻找特定排列
在本例中string类也可以像数组一样使用下角标 #include <iostream> #include <cstdlib> #include <vector> ...
- sqldbx连不上oracle,SqlDbx连接oracle(无需安装Oracle客户端)
下载地址:https://download.csdn.net/download/xzplinke/10428957 解压SqlDbx.zip,将SqlDbx放到C:盘根目录(C:\SqlDbx 路径是 ...
- c++STL算法基础
STL算法基础 算法概述 STL中算法分类 查找算法(13个) adjacent_find binary_search count count_if equal_range find find_end ...
- 最全ACM常用STL
STL 中专门用于排列的函数(可以处理存在重复数据集的排列问题) 头文件:#include <algorithm> using namespace std; 调用: next_permut ...
最新文章
- iOS原生定位和反编码
- golang单向散列函数
- 学习笔记(16):Python网络编程并发编程-开启子进程的两种方式
- django 各种包的集合网站
- 下列关于java集合说法错误的是_下列关于Spring的说法错误的是( )。
- Vue 字符串转JSON
- for 循环中实现多个点击事件
- 基于u-boot源码的简单shell软件实现
- android壁纸路径/目录
- 中文版eclipse,格式改成UTF-8
- IoTDB MPP框架源码解读之SQL的一生(襁褓)
- javascript手册安卓版_JavaScript 手册
- 基于FPGA的SDRAM控制器设计(4)
- Latex下载安装配置
- maven-publish插件的使用笔记
- everedit 保存机器学习路径注意事项
- 天使爱美丽经典台词语录片段对白分享
- 【Git】git did not exit cleanly (exit code 1) 异常处理
- Python基础知识-pycharm版-对象
- 实现a标签中的各种点击(onclick)事件的方法