C++中std::reverse和std::reverse_copy的使用
来源:互联网 发布:淘宝花呗店铺出售 编辑:程序博客网 时间:2024/05/21 09:24
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 totemplate <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/reverseint 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_copyint 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
0 1
- C++中std::reverse和std::reverse_copy的使用
- std::function和std::bind的使用?
- std::reverse
- std::bind绑定器、std::function、extern C的使用
- numpy中std()和pandas中std()的区别
- C++/C++11中std::transform的使用
- C++/C++11中std::queue的使用
- C++/C++11中std::priority_queue的使用
- C++/C++11中std::deque的使用
- C++/C++11中std::stack的使用
- C++/C++11中std::numeric_limits的使用
- C++/C++11中std::exception的使用
- C++/C++11中std::runtime_error的使用
- C++中std::fill/std::fill_n的使用
- C++11 中std::function和std::bind的用法
- C++11 中std::thread和std::mutex的用法
- C++11 中std::function和std::bind的用法
- 使用std::function和std::bind写的观察者模式
- JBoss7/WildFly配置数据源:mysql
- Dockerfile中ENTRYPOINT 和 CMD的区别
- 事件冒泡相关处理
- 虚函数和多态
- 考试篇(5.2) NSE4 题库 02. 日志与监控 ❀ 飞塔 (Fortinet) 网络安全专家
- C++中std::reverse和std::reverse_copy的使用
- ubuntu16.04输入密码后登入黑屏
- scrapy 登录
- R语言向量_NA与NULL值
- 用vue手脚架生成的项目修.vue文件后,保存编译报错,缺少2个空格
- leetcode_303. Range Sum Query
- 【Bzoj3668】起床困难综合症
- 网络信息服务 (NIS/YP)
- Java开发环境搭建