作用:倒置或调换区间元素的位置

声明:

  1. #include <algorithm>
  2. template <class biDirectionalItr>
  3. void reverse(biDirectionalItr first, biDirectionalItr last);
  4. template <class biDirectionalItr, class outputItr>
  5. outputItr reverse_copy(biDirectionalItr first, biDirectionalItr last,outputItr destFirst);
  6. template <class forwardItr>
  7. void rotate(forwardItr first, forwardItr newFirst, forwardItr last);
  8. template <class forwardItr, class outputItr>
  9. outputItr rotate_copy(forwardItr first, forwardItr middle, forwardItr middle,last,outputItr destFirst);

示例代码:

  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 temp[10] = {1,3,5,7,9,0,2,4,6,8};
  12. list<int> intList(temp,temp+10);
  13. list<int> resultList;
  14. list<int>::iterator listItr;
  15. ostream_iterator<int> screen(cout," ");
  16. cout << "intList" << endl;
  17. copy(intList.begin(),intList.end(),screen);
  18. cout << endl;
  19. // 倒置
  20. reverse(intList.begin(),intList.end());
  21. cout << "intList" << endl;
  22. copy(intList.begin(),intList.end(),screen);
  23. cout << endl;
  24. // reverse_copy
  25. reverse_copy(intList.begin(),intList.end(),back_inserter(resultList));
  26. cout << "resultList:" << endl;
  27. copy(resultList.begin(),resultList.end(),screen);
  28. cout << endl;
  29. listItr = intList.begin();
  30. listItr++;
  31. listItr++;
  32. cout << "intList before ratete" << endl;
  33. copy(intList.begin(),intList.end(),screen);
  34. cout << endl;
  35. // rotate
  36. rotate(intList.begin(),listItr,intList.end());
  37. cout << "intList after ratete" << endl;
  38. copy(intList.begin(),intList.end(),screen);
  39. cout << endl;
  40. resultList.clear();
  41. // rotate_copy
  42. rotate_copy(intList.begin(),listItr,intList.end(),back_inserter(resultList));
  43. cout << "intList after ratete_copy" << endl;
  44. copy(intList.begin(),intList.end(),screen);
  45. cout << endl;
  46. cout << "resultList:" << endl;
  47. copy(resultList.begin(),resultList.end(),screen);
  48. cout << endl;
  49. resultList.clear();
  50. // 牛逼闪闪的用法
  51. rotate_copy(intList.begin(),find(intList.begin(),intList.end(),6),intList.end(),back_inserter(resultList));
  52. cout << "resultList:" << endl;
  53. copy(resultList.begin(),resultList.end(),screen);
  54. cout << endl;
  55. return 0;
  56. }

运行结果:

intList
1 3 5 7 9 0 2 4 6 8
intList
8 6 4 2 0 9 7 5 3 1
resultList:
1 3 5 7 9 0 2 4 6 8
intList before ratete
8 6 4 2 0 9 7 5 3 1
intList after ratete
4 2 0 9 7 5 3 1 8 6

STL 之reverse,reverse_copy,rotate,rotate_copy相关推荐

  1. C++ STL 逆转旋转 reverse reverse_copy rotate

    #include <iostream> #include <algorithm> #include <vector> #include <iterator&g ...

  2. stl中copy()函数_std :: rotate_copy()函数以及C ++ STL中的示例

    stl中copy()函数 C ++ STL std :: rotate_copy()函数 (C++ STL std::rotate_copy() function) rotate_copy() fun ...

  3. STL实现细节之rotate()

    引言 STL中 rotate(first, middle, last) 函数的作用是原地把容器区间 [first, middle)(左半部分) 与 [middle, last) (右半部分)的元素互换 ...

  4. STL源码分析-rotate

    http://note.youdao.com/noteshare?id=4ba8ff81aa96373ba11f1b82597ec73a 转载于:https://www.cnblogs.com/tai ...

  5. 最全ACM常用STL

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

  6. ACM竞赛常用STL(二)之STL--algorithm

    <algorithm>无疑是STL 中最大的一个头文件,它是由一大堆模板函数组成的. 下面列举出<algorithm>中的模板函数: adjacent_find / binar ...

  7. STL源码剖析 数值算法 heap算法

    算法 adjacent_find count count_if find find_if find_end for_each generate generate_n includes max_elem ...

  8. ACM 网址和一些建议

    USACO http://ace.delos.com/usacogate 美国著名在线题库,专门为信息学竞赛选手准备 TJU http://acm.tongji.edu.cn/ 同济大学在线题库,唯一 ...

  9. ACM大量习题题库及建议培养计划

    ACM大量习题题库 ACM大量习题题库  现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. USACO h ...

最新文章

  1. 要命的定义函数。。。参数组合。请认真理解!
  2. 读数据库遇到空就进行不下去_如何保证缓存与数据库的双写一致性?
  3. mysql: union / union all / 自定义函数用法详解
  4. react.lazy 路由懒加载_Vue面试题: 如何实现路由懒加载?
  5. php团队奖,PHP生成奖状
  6. SpringBoot集成Activiti Explorer
  7. Helm 3 完整教程(五):Helm 内置对象详解
  8. 前端开发-技术设计文档编写规范
  9. 中科大2021计算机应用数学期末回忆版
  10. android仿微信播放视频播放器,vue DPlayer 仿微信朋友圈视频播放效果
  11. 计算机试卷分析与反思,试卷分析及教学反思
  12. 一枚前端UI组件库 KUI for Vue
  13. 微信公众号嵌套html,VUE项目嵌入微信公众号文章URL
  14. Unity Shader - URP ShadowCast ShadowRecieve - 投影 和 接受阴影
  15. 完数什么意思_完美数是什么?
  16. 怎么建立win7无线热点
  17. 引流,引流脚本,引流软件,微信引流,引流方法交流
  18. 名词从句:主语从句、宾语从句、表语从句、同位语从句
  19. 长沙计算机应届生工资水平,长沙毕业生期望的平均月薪是多少?答案在这里
  20. element-ui简介

热门文章

  1. 静态static关键字修饰成员方法
  2. 列表-使用del关键字从列表删除数据
  3. SpringBoot 配置错误页
  4. php socket 效率,php socket 模型及效率问题
  5. java cmd找不到文件_cmd中输入java找不到文件解决方法
  6. 利用 exe4j 将 JAR 包制成免安装 JDK 的 EXE 文件
  7. 微信公众号文章中图片加载时,占位图宽高大小的确定
  8. 《UML面向对象设计基础》—第1章1.5节消息
  9. vue computed 使用小问题
  10. JavaScript高级程序设计(第三版)学习笔记1~5章