当前位置: 首页 > 网络知识

C++ std::reverse函数

时间:2026-01-31 09:19:32

函数原型,定义

std::reverse

定义于头文件<algorithm>

1 (1) 2 template< class BidirIt > 3 void reverse( BidirIt first, BidirIt last );(C++20 前) 4 5 template< class BidirIt > 6 constexpr void reverse( BidirIt first, BidirIt last );(C++20 起) 7 8 (2) 9 template< class ExecutionPolicy, class BidirIt > 10 void reverse( ExecutionPolicy&``& policy, BidirIt first, BidirIt last ); (C++17 起)

1.反转 [first, last) 范围中的元素顺序
表现如同应用 std::iter_swap 到对于非负 i < (lastfirst)/2 的每对迭代器 first+i, (lasti) 1
2.同 (1) ,但按照 policy 执行。此重载仅若 std::is_execution_policy_v<std::decay_t> 为 true 才参与重载决议。
可能实的现:

1 template<class BidirIt> 2 void reverse(BidirIt first, BidirIt last) 3 7 }

应用实例

1 #include <vector> 2 #include <iostream> 3 #include <iterator> 4 #include <algorithm> 5 6 int main() 7 ; 9 std::reverse(std::begin(v), std::end(v)); 10 for(auto e : v) std::cout << e; 11 std::cout << '\n'; 12 13 int a[] = ; 14 std::reverse(std::begin(a), std::end(a)); 15 for(auto e : a) std::cout << e; 16 } 17 18 输出: 19 321 20 7654



上一篇:C++11标准模板(STL) 算法(std::reverse)
下一篇:system32.bat是什么程序?system32.bat是什么意思
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器
  • 英特尔第五代 Xeon CPU 来了:详细信息和行业反应
  • 由于云计算放缓引发扩张担忧,甲骨文股价暴跌
  • Web开发状况报告详细介绍可组合架构的优点
  • 如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳
  • 美光在数据中心需求增长后给出了强有力的预测
  • 2027服务器市场价值将接近1960亿美元
  • 生成式人工智能的下一步是什么?
  • 分享在外部存储上安装Ubuntu的5种方法技巧
  • 全球数据中心发展的关键考虑因素
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器

    英特尔第五代 Xeon CPU 来了:详细信息和行业反应

    由于云计算放缓引发扩张担忧,甲骨文股价暴跌

    Web开发状况报告详细介绍可组合架构的优点

    如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳

    美光在数据中心需求增长后给出了强有力的预测

    2027服务器市场价值将接近1960亿美元

    生成式人工智能的下一步是什么?

    分享在外部存储上安装Ubuntu的5种方法技巧

    全球数据中心发展的关键考虑因素