STL常用算法: fill,rotate,rotate_copy.
来源:互联网 发布:软件恢复大师 编辑:程序博客网 时间:2024/06/01 19:50
fill算法用来填充容器,当然创建容器的时候,也可以达到与fill算法一样的效果.
#include <iostream>#include <vector>#include <algorithm>using namespace std;//fill算法的手动实现.template <typename T, typename V>void myFill(T first, T last, const V value){ while (first != last) { *first++ = value; }}int main(){ vector<int> s = {1,2,3,4,5,6,7}; //用来让指定范围内的元素填充为5. fill(s.begin(), s.begin() + 3, 5); // 5 5 5 4 5 6 7 //当然这样也是可以的.给容器temp填充了10个5. vector<int> temp(10,5); system("pause"); return 0;}
rotate用来循环容器内的元素.但它和swap_ranges不同,它旋转的元素个数不用相等.
#include <iostream>#include <vector>#include <algorithm>using namespace std;//rotate的实现.template <typename T>void myRotate(T first, T mid, T last){ //这个实现挺有趣的,倒序即可实现. reverse(first, mid); reverse(mid, last); reverse(first, last);}int main(){ vector<int> s = {1,2,3,4,5,6,7}; rotate(s.begin(), s.begin() + 3, s.end()); // 4 5 6 7 1 2 3 system("pause"); return 0;}
既然有rotate,那么肯定有rotate_copy了.
template <typename T>void myRotate_copy(T first, T mid, T last, T new_first){ //一般有copy的算法,都比较简单实现. T temp = mid; while (temp != last) { *new_first = *temp++; } while (first != mid) { *new_first = *first++; }}
阅读全文
0 0
- STL常用算法: fill,rotate,rotate_copy.
- STL algorithm算法rotate,rotate_copy(51)
- STL 之reverse,reverse_copy,rotate,rotate_copy
- STL 之reverse,reverse_copy,rotate,rotate_copy
- STL_算法_旋转(rotate、rotate_copy)
- rotate/rotate_copy详解
- reverse,reverse_copy,rotate,rotate_copy
- STL算法:find、fill
- STL algorithm算法fill(14)
- stl中的rotate算法
- STL的rotate算法
- STL_算法(25)_逆转和旋转 revers() 、reverse_copy()、rotate()、rotate_copy()、
- STL-fill()填充算法以及for_each()算法
- C++标准库---旋转元素rotate()&rotate_copy()
- rotate_copy
- STL经典算法集锦<四>之rotate
- STL 源码剖析 算法 stl_algo.h -- rotate
- STL源码—— rotate算法理解
- 常见异常
- iOS APNS推送
- RMAN Catalog 和 Nocatalog 的区别
- 一文入门Python数据分析库Pandas
- 计算机中的堆和和栈(物理内存上的解释)
- STL常用算法: fill,rotate,rotate_copy.
- 大家好
- 多线程断点下载----java
- 【转载】CentOS 7 安装 Kibana 5.x
- 对比三大内容付费工具,你该如何选择?
- ajax在网络断开时,缓存ajax的调用,网络连接时,恢复ajax调用
- Struts框架demo测试的时候报错:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFil
- SEO站内优化与站外优化有什么区别
- 织梦cms三级栏目权限管理修改