STL 之reverse,reverse_copy,rotate,rotate_copy
作用:倒置或调换区间元素的位置
声明:
- #include <algorithm>
- template <class biDirectionalItr>
- void reverse(biDirectionalItr first, biDirectionalItr last);
- template <class biDirectionalItr, class outputItr>
- outputItr reverse_copy(biDirectionalItr first, biDirectionalItr last,outputItr destFirst);
- template <class forwardItr>
- void rotate(forwardItr first, forwardItr newFirst, forwardItr last);
- template <class forwardItr, class outputItr>
- outputItr rotate_copy(forwardItr first, forwardItr middle, forwardItr middle,last,outputItr destFirst);
示例代码:
- #include <iostream>
- #include <list>
- #include <string>
- #include <numeric>
- #include <iterator>
- #include <vector>
- #include <functional>
- #include <algorithm>
- using namespace std;
- int main() {
- int temp[10] = {1,3,5,7,9,0,2,4,6,8};
- list<int> intList(temp,temp+10);
- list<int> resultList;
- list<int>::iterator listItr;
- ostream_iterator<int> screen(cout," ");
- cout << "intList" << endl;
- copy(intList.begin(),intList.end(),screen);
- cout << endl;
- // 倒置
- reverse(intList.begin(),intList.end());
- cout << "intList" << endl;
- copy(intList.begin(),intList.end(),screen);
- cout << endl;
- // reverse_copy
- reverse_copy(intList.begin(),intList.end(),back_inserter(resultList));
- cout << "resultList:" << endl;
- copy(resultList.begin(),resultList.end(),screen);
- cout << endl;
- listItr = intList.begin();
- listItr++;
- listItr++;
- cout << "intList before ratete" << endl;
- copy(intList.begin(),intList.end(),screen);
- cout << endl;
- // rotate
- rotate(intList.begin(),listItr,intList.end());
- cout << "intList after ratete" << endl;
- copy(intList.begin(),intList.end(),screen);
- cout << endl;
- resultList.clear();
- // rotate_copy
- rotate_copy(intList.begin(),listItr,intList.end(),back_inserter(resultList));
- cout << "intList after ratete_copy" << endl;
- copy(intList.begin(),intList.end(),screen);
- cout << endl;
- cout << "resultList:" << endl;
- copy(resultList.begin(),resultList.end(),screen);
- cout << endl;
- resultList.clear();
- // 牛逼闪闪的用法
- rotate_copy(intList.begin(),find(intList.begin(),intList.end(),6),intList.end(),back_inserter(resultList));
- cout << "resultList:" << endl;
- copy(resultList.begin(),resultList.end(),screen);
- cout << endl;
- return 0;
- }
运行结果:
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相关推荐
- C++ STL 逆转旋转 reverse reverse_copy rotate
#include <iostream> #include <algorithm> #include <vector> #include <iterator&g ...
- stl中copy()函数_std :: rotate_copy()函数以及C ++ STL中的示例
stl中copy()函数 C ++ STL std :: rotate_copy()函数 (C++ STL std::rotate_copy() function) rotate_copy() fun ...
- STL实现细节之rotate()
引言 STL中 rotate(first, middle, last) 函数的作用是原地把容器区间 [first, middle)(左半部分) 与 [middle, last) (右半部分)的元素互换 ...
- STL源码分析-rotate
http://note.youdao.com/noteshare?id=4ba8ff81aa96373ba11f1b82597ec73a 转载于:https://www.cnblogs.com/tai ...
- 最全ACM常用STL
STL 中专门用于排列的函数(可以处理存在重复数据集的排列问题) 头文件:#include <algorithm> using namespace std; 调用: next_permut ...
- ACM竞赛常用STL(二)之STL--algorithm
<algorithm>无疑是STL 中最大的一个头文件,它是由一大堆模板函数组成的. 下面列举出<algorithm>中的模板函数: adjacent_find / binar ...
- STL源码剖析 数值算法 heap算法
算法 adjacent_find count count_if find find_if find_end for_each generate generate_n includes max_elem ...
- ACM 网址和一些建议
USACO http://ace.delos.com/usacogate 美国著名在线题库,专门为信息学竞赛选手准备 TJU http://acm.tongji.edu.cn/ 同济大学在线题库,唯一 ...
- ACM大量习题题库及建议培养计划
ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. USACO h ...
最新文章
- 要命的定义函数。。。参数组合。请认真理解!
- 读数据库遇到空就进行不下去_如何保证缓存与数据库的双写一致性?
- mysql: union / union all / 自定义函数用法详解
- react.lazy 路由懒加载_Vue面试题: 如何实现路由懒加载?
- php团队奖,PHP生成奖状
- SpringBoot集成Activiti Explorer
- Helm 3 完整教程(五):Helm 内置对象详解
- 前端开发-技术设计文档编写规范
- 中科大2021计算机应用数学期末回忆版
- android仿微信播放视频播放器,vue DPlayer 仿微信朋友圈视频播放效果
- 计算机试卷分析与反思,试卷分析及教学反思
- 一枚前端UI组件库 KUI for Vue
- 微信公众号嵌套html,VUE项目嵌入微信公众号文章URL
- Unity Shader - URP ShadowCast ShadowRecieve - 投影 和 接受阴影
- 完数什么意思_完美数是什么?
- 怎么建立win7无线热点
- 引流,引流脚本,引流软件,微信引流,引流方法交流
- 名词从句:主语从句、宾语从句、表语从句、同位语从句
- 长沙计算机应届生工资水平,长沙毕业生期望的平均月薪是多少?答案在这里
- element-ui简介