C++中std::reverse和std::reverse_copy的使用
std::reverse:反转排序容器内指定范围中的元素。
std::reverse_copy与std::reverse唯一的区别是:reverse_copy会将结果拷贝到另外一个容器中,而不影响原容器的内容。
std::reverse: defined in header <algorithm>, reverses the order of the elements in the range [first, last).
std::reverse_copy: defined in header <algorithm>, copies the elements in the range[first,last) to the range beginning at result, but in reverse order.
下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:
#include "reverse.hpp"
#include <iostream>
#include <algorithm> // std::reverse/std::reverse_copy
#include <vector>
#include <string>/* The behavior of this function template(std::reverse) is equivalent to
template <class BidirectionalIterator>
void reverse (BidirectionalIterator first, BidirectionalIterator last)
{while ((first!=last)&&(first!=--last)) {std::iter_swap (first,last);++first;}
} *//* The behavior of this function template(std::reverse_copy) is equivalent to:
template <class BidirectionalIterator, class OutputIterator>
OutputIterator reverse_copy (BidirectionalIterator first,BidirectionalIterator last, OutputIterator result)
{while (first!=last) {--last;*result = *last;++result;}return result;
} *//
// reference: http://en.cppreference.com/w/cpp/algorithm/reverse
int test_reverse_1()
{std::vector<int> v({ 1, 2, 3 });std::reverse(std::begin(v), std::end(v));std::cout << v[0] << v[1] << v[2] << '\n';int a[] = { 4, 5, 6, 7 };std::reverse(std::begin(a), std::end(a));std::cout << a[0] << a[1] << a[2] << a[3] << '\n';return 0;
}/
// reference: http://www.cplusplus.com/reference/algorithm/reverse/
int test_reverse_2()
{std::vector<int> myvector;// set some values:for (int i = 1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9//std::reverse(myvector.begin(), myvector.end()); // 9 8 7 6 5 4 3 2 1std::reverse(myvector.begin(), myvector.begin()+5); // 5 4 3 2 1 6 7 8 9// print out content:std::cout << "myvector contains:";for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';std::string str{ "abcdef" };std::reverse(str.begin(), str.end());fprintf(stderr, "str: %s\n", str.c_str());return 0;
}/
// reference: http://www.cplusplus.com/reference/algorithm/reverse_copy/
int test_reverse_copy_1()
{int myints[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };std::vector<int> myvector;myvector.resize(5); // allocate spacestd::reverse_copy(myints, myints + 5, myvector.begin());// print out content:std::cout << "myvector contains:";for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0;
}//
// reference: http://en.cppreference.com/w/cpp/algorithm/reverse_copy
int test_reverse_copy_2()
{std::vector<int> v({ 1, 2, 3 });for (const auto& value : v) {std::cout << value << " ";}std::cout << '\n';std::vector<int> destination(3);std::reverse_copy(std::begin(v), std::end(v), std::begin(destination));for (const auto& value : destination) {std::cout << value << " ";}std::cout << '\n';return 0;
}
GitHub: https://github.com/fengbingchun/Messy_Test
C++中std::reverse和std::reverse_copy的使用相关推荐
- std::reverse 用法
描述: 逆转容器中的元素顺序. 定义: template< class BidirIt > void reverse( BidirIt first, BidirIt last ...
- C++中标准模板库std::vector的实现
以下实现了C++标准模板库std::vector的部分实现,参考了 cplusplus. 关于C++中标准模板库std::vector的介绍和用法可以参考 https://blog.csdn.net/ ...
- C++中标准模板库std::pair的实现
以下用C++实现了标准模板库中的std::pair实现,参考了 cplusplus 和 vs2013中的utility文件. 关于std::pair的介绍和用法可以参考: https://blog.c ...
- C++中std::function和std::bind
1.可调用对象 可调用对象有一下几种定义: 是一个函数指针,参考 C++ 函数指针和函数类型: 是一个具有operator()成员函数的类的对象: 可被转换成函数指针的类对象: 一个类成员函数指针: ...
- C++演示std :: sort(),std :: reverse()的函数(附完整源码)
@[TOC](C++演示std :: sort(),std :: reverse()的函数) C++演示std :: sort(),std :: reverse()的函数完整源码(定义,实现,main ...
- [C/C++]关于C++11中的std::move和std::forward
http://blog.sina.com.cn/s/blog_53b7ddf00101p5t0.html std::move是一个用于提示优化的函数,过去的c++98中,由于无法将作为右值的临时变量从 ...
- C++并发中的条件变量 std::condition_variable
简介 这个操作相当于操作系统中的Wait & Signal原语,程序中的线程根据实际情况,将自己阻塞或者唤醒其他阻塞的线程. 个人认为,条件变量的作用在于控制线程的阻塞和唤醒,这需要和锁进行相 ...
- C++11中的时间库std::chrono(引发关于时间的思考)
文章目录 前言 chrono 的概况 chrono 的核心内容 duration clock system_clock steady_clock time point 关于时间的思考 时间函数思考 总 ...
- c语言中std::map_在现代C ++中明智地使用std :: map
c语言中std::map std::map and its siblings(std::multimap, std::unordered_map/multimap) used to be my fav ...
最新文章
- yum卸载遇到的问题--待解决
- HDU2076 夹角有多大
- as3.0用了视频组件,导致视频打开后就全屏,加一下代码就行
- Rocky(dfs)
- android:allowBackup=true 数据备份(adb backup)+查看(abe unpack backup.ab backup.tar)+恢复(adb re)
- Function!(计蒜客 - 42386)
- 云漫圈 | 什么是微服务?
- selenium-webdriver——如何在启动firefox时加载扩展
- 决不允许AI杀人武器研发!马斯克领衔2400名科学家签署联名宣言
- UI(用户界面)设计规则和规范
- Atitit.数据库分区的设计 attilax 总结
- java证书不见了_java – 找不到证书链
- 安卓软件安装包后缀名_安卓手机安装包是什么格式?
- linux服务器怎么拷贝文件,linux 服务器之间拷贝文件
- 【转载】巴菲特:比能力更重要的是靠谱
- 动感校园行17951长途ip电话卡
- linux涂鸦软件,绘图应用程序:Pinta,Krita,Tux Paint,Drawpile,MyPaint,KolourPaint
- Android谷歌地图地理编码,谷歌地图API地理编码多个地点
- R语言多元Logistic逻辑回归 应用案例
- 高中数学怎么学好如何轻松学好高中数学
热门文章
- 数字图像处理:(1)图像梯度以及算子应用
- java 宽字节_宽字节注入
- GVINS:基于GNSS视觉惯性紧耦合的平滑状态估计方法
- 在CentOS 7.7 x86_64上安装python3.7.7
- 在CentOS 6.3 64bit上安装Apache Trafficserver 4.2.3挂载SAS硬盘和SSD
- Ubuntu 14.04 64bit上curl-7.37源码包中的sample 源码示例研究
- Linux下查看Nginx,tomcat等的并发连接数和连接状态
- 使用VS Code开发.Net Core 2.0 MVC Web应用程序教程之三(配置文件读取)
- 3.分支结构与循环结构
- 周记 2016.4.5