C++ 标准库中的 reverse 函数

C++ 标准库中的 reverse 函数

在C++标准库中,reverse函数用于反转序列。它定义在< algorithm >头文件中。以下是详细说明:

一.函数原型

template<classBidirectionalIterator>voidreverse(BidirectionalIterator first, BidirectionalIterator last);

二.函数参数

first:指向要反转序列起始位置的迭代器
last:指向要反转序列结束位置的下一个位置的迭代器(左闭右开区间 [first, last))

三.使用示例

1.反转数组

#include<iostream>#include<algorithm>intmain(){int arr[]={1,2,3,4,5};int n =sizeof(arr)/sizeof(arr[0]); std::reverse(arr, arr + n);for(int i =0; i < n; i++){ std::cout << arr[i]<<" ";// 输出: 5 4 3 2 1}return0;}

2.反转vector

#include<iostream>#include<algorithm>#include<vector>usingnamespace std;intmain(){ vector<int> vec ={1,2,3,4,5};reverse(vec.begin(), vec.end());for(int num : vec){ cout << num <<" ";// 输出: 5 4 3 2 1}return0;}

3.反转string

#include<iostream>#include<algorithm>#include<string>usingnamespace std;intmain(){ string str ="Hello, World!";reverse(str.begin(), str.end()); cout << str << endl;// 输出: !dlroW ,olleHreturn0;}

4,反转部分元素

#include<iostream>#include<algorithm>#include<vector>usingnamespace std;intmain(){ vector<int> vec ={1,2,3,4,5,6,7,8};// 只反转中间部分元素 [2, 3, 4, 5, 6] -> [6, 5, 4, 3, 2]reverse(vec.begin()+1, vec.end()-1);for(int num : vec){ cout << num <<" ";// 输出: 1 7 6 5 4 3 2 8}return0;}

四.复杂度分析

时间复杂度:O(n),其中 n 是 last - first,执行大约 n/2 次交换
空间复杂度:O(1),原地操作,不需要额外空间

五.注意事项

  1. reverse 函数要求迭代器是双向迭代器(BidirectionalIterator)。
  2. 可以用于所有支持双向迭代器的容器:vectordequeliststring数组
  3. reverse 会修改原容器,如果不希望修改原容器,可以使用 reverse_copy

六.相关函数

1.reverse_copy

reverse函数不保证稳定性(因为交换元素可能会改变相等元素的相对顺序,但通常我们使用reverse时并不关心这个,因为元素值不同,且即使相同,反转后顺序也变了)。
C++标准库还提供了reverse_copy函数,它可以将反转的结果复制到另一个序列中,而不改变原序列。

  • reverse_copy的函数原型:
template<classBidirectionalIterator,classOutputIterator> OutputIterator reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
  • 使用示例
#include<iostream>#include<algorithm>#include<vector>usingnamespace std;intmain(){ vector<int> src ={1,2,3,4,5}; vector<int>dst(src.size());reverse_copy(src.begin(), src.end(), dst.begin()); cout <<"原序列: ";for(int num : src){ cout << num <<" ";// 输出: 1 2 3 4 5} cout <<"\n反转后的副本: ";for(int num : dst){ cout << num <<" ";// 输出: 5 4 3 2 1}return0;}

2.自定义反转算法实现

#include<iostream>#include<vector>usingnamespace std;// 手动实现 reverse 功能template<typenameT>voidmy_reverse(T begin, T end){while(begin != end && begin !=--end){swap(*begin,*end);++begin;}}intmain(){ vector<int> vec ={1,2,3,4,5};my_reverse(vec.begin(), vec.end());for(int num : vec){ cout << num <<" ";// 输出: 5 4 3 2 1}return0;}

3.与反向迭代器的区别

#include<iostream>#include<vector>#include<algorithm>usingnamespace std;intmain(){ vector<int> vec ={1,2,3,4,5};// 使用 reverse 函数修改原容器reverse(vec.begin(), vec.end()); cout <<"使用 reverse 后: ";for(int num : vec){ cout << num <<" ";// 输出: 5 4 3 2 1} cout << endl;// 重置 vector vec ={1,2,3,4,5};// 使用反向迭代器(不修改原容器,只是反向遍历) cout <<"使用反向迭代器遍历: ";for(auto it = vec.rbegin(); it != vec.rend();++it){ cout <<*it <<" ";// 输出: 5 4 3 2 1} cout << endl; cout <<"原容器未被修改: ";for(int num : vec){ cout << num <<" ";// 输出: 1 2 3 4 5}return0;}

Read more

Mac Mini:开发环境配置指南:从零安装 Homebrew、Python、Git、Node 和 UV 工具

文章目录 * 前言 * 一、Homebrew 安装与配置 * 1. 安装 Homebrew * 2. 配置环境变量 * 3. 验证安装 * 4. 常见问题解决 * 二、Python 环境配置 * 1. 通过 Homebrew 安装 Python * 2. 验证安装 * 3. 虚拟环境管理 * 三、Git 版本控制工具 * 1. 安装 Git * 2. 基本配置 * 3. 验证安装 * 四、Node.js 安装与管理 * 1. 通过 Homebrew 安装 Node.js * 2. 验证安装 * 3. 使用

By Ne0inhk

tmux_for_windows windows上面安装 git bash 2026年 正是专门为了解决“SSH 掉线后还能继续原来的会话”这个问题而设计的工具

tmux_for_windows tmux是一个开源工具,用于在一个终端窗口中运行多个终端会话。本工具从msys2里提取,可以在Git for Windows的Git Bash (MingW64)下正常使用。 蘭雅sRGB 龙芯小本服务器 | https://262235.xyz ##tmux(以及 screen)正是专门为了解决“SSH 掉线后还能继续原来的会话”这个问题而设计的工具。 简单来说: 工具SSH 掉线后还能连上原来的会话吗?说明普通 bash / zsh不能会话结束,进程收到 SIGHUP 信号,通常被杀死tmux能会话独立于 SSH 连接存在,掉线后可以随时重新 attachscreen能和 tmux 功能几乎一样,但 tmux 现在更流行、功能更强zellij能更现代的替代品,但普及度不如 tmux tmux 的典型用法(SSH 掉线后重连)

By Ne0inhk
DeerFlow 2.0开源

DeerFlow 2.0开源

目录 从研究工具到超级智能体 什么是 DeerFlow? 核心特性 技能系统 子智能体 沙盒与文件系统 上下文工程 长期记忆 技术架构 快速开始 推荐模型 应用场景 项目现状 总结 2026 年 2 月 28 日,一款来自字节跳动的开源项目登顶 GitHub Trending 榜首。 它叫 DeerFlow,一个超级智能体架构,短时间就拿了 2.2 万 Star。 说实话,刚看到这个项目的时候,我还有些疑惑——又一个 AI Agent 框架?但深入了解后,我发现这玩意儿确实不一样。 从研究工具到超级智能体 DeerFlow 的故事挺有意思。 最开始,它只是个深度研究框架。但开发者们把它用出了各种花样:构建数据管道、

By Ne0inhk