reverse_copy() 算法可以将源序列复制到目的序列中,目的序列中的元素是逆序的。定义源序列的前两个迭代器参数必须是双向迭代器。目的序列由第三个参数指定,它是目的序列的开始迭代器,也是一个输出迭代器。如果序列是重叠的,函数的行为是未定义的。这个算法会返回一个输出迭代器,它指向目的序列最后一个元素的下一个位置。

本文作者原创,转载请附上文章出处与本文链接。

C++ reverse()函数用法详解(深入了解,一文学会)目录

1 reverse

1.1 反转Vector容器

1.2 反转string

1.3 翻转字符数组

1.4 反转数组

2 reverse_copy()


1 reverse

1.1 反转Vector容器


#include <iostream>
#include <vector>using namespace std;
int main()
{vector<int> a;for (int i = 0; i < 10; i++){a.push_back(i);}cout << "翻转前: " << "\n";for (int i = 0; i < 10; i++){cout << a[i];}reverse(a.begin(), a.end());cout << "\n" << "翻转后: " << "\n";for (int i = 0; i < 10; i++){cout << a[i];}
}

1.2 反转string

#include <iostream>
#include <vector>using namespace std;
int main()
{string s = "abcdefg";cout << "翻转前: " << "\n";cout << s;reverse(s.begin(), s.end());cout << "\n" << "翻转后: " << "\n";cout << s;}

1.3 翻转字符数组

#include <iostream>
#include <vector>using namespace std;
int main()
{char s[] = "abcdefg";int N = sizeof(s) / sizeof(s[0]);cout << "翻转前: " << "\n";cout << s;reverse(s, s + N - 1);cout << "\n" << "翻转后: " << "\n";cout << s;}

1.4 反转数组


#include <iostream>
#include <vector>using namespace std;
int main()
{int s[] = { 1,2,3,4,5,6,7,8,9 };cout << "翻转前: " << "\n";for (int i = 0; i < sizeof(s) / sizeof(s[0]); i++)cout << s[i] << " ";//输出9 8 7 6 5 4 3 2 1reverse(s, s + 9);cout << "\n" << "翻转后: " << "\n";for (int i = 0; i < sizeof(s) / sizeof(s[0]); i++)cout << s[i] << " ";//输出9 8 7 6 5 4 3 2 1}

2 reverse_copy()

reverse_copy函数和reverse函数的唯一区别在于:reverse_copy会将结果拷贝到另外一个容器中,不影响原容器的内容。

reverse_copy(sourceBeg,sourceEnd,destBeg)

reverse_copy()会将源区间[sourceBeg,sourceEnd)内的元素复制到"以destBeg起始的目标区间",并在复制过程中颠倒安置次序;(注:原数组不变)

这里只放了一个例子,其他例子也类似

// Testing for palindromes using reverse_copy()
#include <iostream>                                      // For standard streams
#include <iterator>                                      // For stream iterators and begin() and end()
#include <algorithm>                                     // For reverse_copy() and copy_if()
#include <cctype>                                        // For toupper() and isalpha()
#include <string>
using namespace std;
using std::string;
int main()
{string first, second;first = "123456789";second.resize(first.size());reverse_copy(first.begin(), first.end(), second.begin());cout << second << endl;}

C++ reverse()函数用法详解(深入了解,一文学会)相关推荐

  1. C++ search()函数用法详解(深入了解,一文学会)

    find_end() 函数用于在序列 A 中查找序列 B 最后一次出现的位置.那么,如果想知道序列 B 在序列 A 中第一次出现的位置,该如何实现呢?可以借助 search() 函数. search( ...

  2. ROW_NUMBER() OVER()函数用法详解 (分组排序 例子多)

    ROW_NUMBER() OVER()函数用法详解 (分组排序 例子多) https://blog.csdn.net/qq_25221835/article/details/82762416 post ...

  3. C++中substr()函数用法详解

    C++中substr()函数用法详解 原型: string substr (size_t pos = 0, size_t len = npos) const; 返回一个新构造的string对象,其值初 ...

  4. LayoutInflater的inflate函数用法详解

    LayoutInflater的inflate函数用法详解 LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: ...

  5. c++ memset 语言_C++中memset函数用法详解

    本文实例讲述了C++中memset函数用法.分享给大家供大家参考,具体如下: 功 能: 将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值,块的大小由第三个参数指定,这个函数通常 ...

  6. mysql: union / union all / 自定义函数用法详解

    mysql: union / union all http://www.cnblogs.com/wangyayun/p/6133540.html mysql:自定义函数用法详解 http://www. ...

  7. python中mat函数_Python中flatten( )函数及函数用法详解

    flatten()函数用法 flatten是numpy.ndarray.flatten的一个函数,即返回一个一维数组. flatten只能适用于numpy对象,即array或者mat,普通的list列 ...

  8. ROW_NUMBER() OVER()函数用法详解

    今天同事问了一个关于插入表的问题,对象:被插入表sys_equi_disorg   A  , 查询表sys_equi_dict   B 因为A表的ID不是自增的,并且不能更改表结构,主键默认值还是0, ...

  9. python中import re_Python3中正则模块re.compile、re.match及re.search函数用法详解

    本文实例讲述了Python3中正则模块re.compile.re.match及re.search函数用法.分享给大家供大家参考,具体如下: re模块 re.compile.re.match. re.s ...

最新文章

  1. 深度学习前人精度很高了,该怎么创新?
  2. python2.7虚拟环境virtualenv安装及使用
  3. [前端记录] --- vue axios 等调用完再执行后面的语句
  4. hystrix源码小贴士之中断
  5. java知识点(记录用)
  6. 量子计算时代到来,摩尔定律将要失效?
  7. 廖雪峰js教程笔记11 操作DOM(包含作业)
  8. 基于QT实现的数独游戏DPLL的SAT求解器设计
  9. 原生android字体,安卓原生字体
  10. Tree Booster 的参数
  11. 一生一代一双人:我与51CTO学院的情缘----写于51CTO学院2周年庆
  12. 程序员薪酬高达 462 万元:是怎么回事?
  13. 华为OD机试用Python实现 -【查找树中的元素 or 查找二叉树节点】(2023-Q1 新题)
  14. 公关战之下,分裂的今日头条
  15. Python量化交易学习笔记(21)——A股股票列表更新
  16. 搜狗微信APP逆向(二)so层
  17. SVN 具体某一行代码是谁添加的
  18. 硕士卖房,到底是行业内卷,还是自我突破
  19. 前端技能树,面试复习第 54 天—— 手写代码:情景题
  20. day09_类,对象,封装(学习自用)

热门文章

  1. 淘宝批量下载图片方法
  2. 化繁为简,聊一聊复制状态机系统架构抽象
  3. java线程状态——java线程状态图
  4. jrtplib学习目录及总结
  5. 网站服务器如何防护攻击?网站服务器被挂马如何检测
  6. 华为服务器gpu芯片怎么样,云服务器gpu有多大
  7. 诺丁汉郡议会与Rimini Street续签任务关键型SAP应用程序的支持服务协议
  8. Linux利用filename=${fileuser:-filename}设置文档名
  9. 曼哈顿距离,欧式距离,余弦距离
  10. 使用Itextsharp编辑PDF