作用:用来查找元素和元素排序

声明:

  1. #include <algorithm>
  2. template <class forwardItr1, class forwardItr2>
  3. forwardItr1 search(forwardItr1 first1, forwardItr1 last1,forwardItr2 first2,forwardItr2 last2);
  4. template <class forwardItr1, class forwardItr2,class binaryPredicate>
  5. forwardItr1 search(forwardItr1 first1, forwardItr1 last1,forwardItr2 first2,forwardItr2 last2,binaryPredicate op);
  6. template <class forwardItr, class size,class Type>
  7. forwardItr search_n(forwardItr first, forwardItr last,size count,const Type& value);
  8. template <class forwardItr, class size,class Type,class binaryPredicate>
  9. forwardItr search_n(forwardItr first, forwardItr last,size count,const Type& value,binaryPredicate op);
  10. template<class randomAccessItr>
  11. void sort(randomAccessItr first,randomAccessItr last);
  12. template<class randomAccessItr, class compare>
  13. void sort(randomAccessItr first, randomAccessItr last, compare op);
  14. template<class forwardItr,class Type>
  15. bool binary_search(forwardItr first,forwardItr last,const Type& searchValue);
  16. template<class forwardItr,class Type,class compare>
  17. bool binary_search(forwardItr first, forwardItr last, const Type& searchValue,compare op);

示例代码:

  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4. #include <numeric>
  5. #include <iterator>
  6. #include <vector>
  7. #include <functional>
  8. #include <algorithm>
  9. using namespace std;
  10. int main() {
  11. int intList[15] = {12,34,56,34,34,
  12. 78,38,43,12,25,
  13. 34,56,62,5,49};
  14. vector<int> vecList(intList,intList+15);
  15. int list[2] = {34, 56};
  16. vector<int>::iterator location;
  17. ostream_iterator<int> screen(cout, " ");
  18. cout << "vecList:" << endl;
  19. copy(vecList.begin(),vecList.end(),screen);
  20. cout << endl;
  21. cout << "list:" << endl;
  22. copy(list,list+2,screen);
  23. cout << endl;
  24. // search:查找一个集合是否在另一集合中
  25. // 与find的区别是,find是查找某一个元素
  26. location = search(vecList.begin(),vecList.end(),list,list + 2);
  27. if (location != vecList.end())
  28. {
  29. cout << "location:" << (location - vecList.begin()) << endl;
  30. } else {
  31. cout << "list is not in vecList" << endl;
  32. }
  33. // search_n :查找某个元素第n此出现的位置
  34. location = search_n(vecList.begin(),vecList.end(),2,34);
  35. if (location != vecList.end())
  36. {
  37. cout << "location:" << (location - vecList.begin()) << endl;
  38. } else {
  39. cout << "list is not in vecList" << endl;
  40. }
  41. // sort
  42. sort(vecList.begin(),vecList.end());
  43. cout << "vecList:" << endl;
  44. copy(vecList.begin(),vecList.end(),screen);
  45. cout << endl;
  46. bool found;
  47. // 用二分法查找:前提先排序
  48. found = binary_search(vecList.begin(),vecList.end(),43);
  49. if (found)
  50. {
  51. cout << "43 found in vecList." << endl;
  52. } else {
  53. cout << "43 not found in vecList." << endl;
  54. }
  55. return 0;
  56. }

运行结果:

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相关推荐

  1. C++的STL库,vector sort排序时间复杂度 及常见容器比较

    http://www.cnblogs.com/sthv/p/5511921.html http://www.169it.com/article/3215620760.html http://www.c ...

  2. c语言的erase函数,C++ STL标准库中sort,erase,remove_if函数的使用

    本篇介绍C++ STL中常用的几个函数 1.std::sort 1.1.sort函数包含在头文件为#include的c++标准库中,调用标准库里的排序方法可以实现对数据的排序. 1.2.sort函数的 ...

  3. 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 ...

  4. c++ STL find search

    #include <iostream> #include <algorithm> #include <deque> #include <list> #i ...

  5. C++STL算法 search你是我的半截的诗

    wchar_t这种宽字符型变量在打印的时候不能像下面那样直接打出来, copy(wvec.begin(), wvec.end(), ostream_iterator<int>(cout, ...

  6. C++STL算法 search 在圆周率中寻找特定排列

    在本例中string类也可以像数组一样使用下角标 #include <iostream> #include <cstdlib> #include <vector> ...

  7. sqldbx连不上oracle,SqlDbx连接oracle(无需安装Oracle客户端)

    下载地址:https://download.csdn.net/download/xzplinke/10428957 解压SqlDbx.zip,将SqlDbx放到C:盘根目录(C:\SqlDbx 路径是 ...

  8. c++STL算法基础

    STL算法基础 算法概述 STL中算法分类 查找算法(13个) adjacent_find binary_search count count_if equal_range find find_end ...

  9. 最全ACM常用STL

    STL 中专门用于排列的函数(可以处理存在重复数据集的排列问题) 头文件:#include <algorithm> using namespace std; 调用: next_permut ...

最新文章

  1. iOS原生定位和反编码
  2. golang单向散列函数
  3. 学习笔记(16):Python网络编程并发编程-开启子进程的两种方式
  4. django 各种包的集合网站
  5. 下列关于java集合说法错误的是_下列关于Spring的说法错误的是( )。
  6. Vue 字符串转JSON
  7. for 循环中实现多个点击事件
  8. 基于u-boot源码的简单shell软件实现
  9. android壁纸路径/目录
  10. 中文版eclipse,格式改成UTF-8
  11. IoTDB MPP框架源码解读之SQL的一生(襁褓)
  12. javascript手册安卓版_JavaScript 手册
  13. 基于FPGA的SDRAM控制器设计(4)
  14. Latex下载安装配置
  15. maven-publish插件的使用笔记
  16. everedit 保存机器学习路径注意事项
  17. 天使爱美丽经典台词语录片段对白分享
  18. 【Git】git did not exit cleanly (exit code 1) 异常处理
  19. Python基础知识-pycharm版-对象
  20. 实现a标签中的各种点击(onclick)事件的方法

热门文章

  1. ES6新特性之let和const命令
  2. Nginx_反向代理配置讲解
  3. RSA签名算法 - Java加密与安全
  4. Disruptor并发框架-1
  5. 解决 IntelliJ IDEA 内置的 Tomcat 日志中显示的中文乱码
  6. Makefile(直接可以使用)
  7. Allegro光绘的导出
  8. 贴片电容耐压值一般都是多少?
  9. 超详细 Spring Boot 知识清单
  10. How is javascript asynchronous AND single threaded?