Namespaces
Variants

std::ranges:: reverse

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
定义于头文件 <algorithm>
调用签名
template < std:: bidirectional_iterator I, std:: sentinel_for < I > S >

requires std:: permutable < I >
constexpr I

reverse ( I first, S last ) ;
(1) (自 C++20 起)
template < ranges:: bidirectional_range R >

requires std:: permutable < ranges:: iterator_t < R >>
constexpr ranges:: borrowed_iterator_t < R >

reverse ( R && r ) ;
(2) (自 C++20 起)
1) 反转范围 [ first , last ) 中元素的顺序。
行为如同对每个整数 i 应用 ranges::iter_swap 到迭代器对 first + i, last - i - 1 ,其中 0 ≤ i < ( last - first ) / 2
2) (1) 相同,但使用 r 作为范围,如同以 ranges:: begin ( r ) 作为 first ,并以 ranges:: end ( r ) 作为 last

本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:

目录

参数

first, last - 定义待反转元素 范围 的迭代器-哨位对
r - 待反转的元素范围

返回值

一个等于 last 的迭代器。

复杂度

恰好进行 ( last - first ) / 2 次交换。

注释

实现(例如 MSVC STL )可在迭代器类型建模为 contiguous_iterator 且交换其值类型时既不调用非平凡特殊成员函数,也不调用通过 ADL 查找到的 swap 时启用向量化。

可能的实现

另请参阅 libstdc++ MSVC STL 中的实现。

struct reverse_fn
{
    template<std::bidirectional_iterator I, std::sentinel_for<I> S>
    requires std::permutable<I>
    constexpr I operator()(I first, S last) const
    {
        auto last2 {ranges::next(first, last)};
        for (auto tail {last2}; !(first == tail or first == --tail); ++first)
            ranges::iter_swap(first, tail);
        return last2;
    }
    template<ranges::bidirectional_range R>
    requires std::permutable<ranges::iterator_t<R>>
    constexpr ranges::borrowed_iterator_t<R>
        operator()(R&& r) const
    {
        return (*this)(ranges::begin(r), ranges::end(r));
    }
};
inline constexpr reverse_fn reverse {};

示例

#include <algorithm>
#include <array>
#include <iostream>
#include <string>
int main()
{
    std::string s {"ABCDEF"};
    std::cout << s << " → ";
    std::ranges::reverse(s.begin(), s.end());
    std::cout << s << " → ";
    std::ranges::reverse(s);
    std::cout << s << " │ ";
    std::array a {1, 2, 3, 4, 5};
    for (auto e : a)
        std::cout << e << ' ';
    std::cout << "→ ";
    std::ranges::reverse(a);
    for (auto e : a)
        std::cout << e << ' ';
    std::cout << '\n';
}

输出:

ABCDEF → FEDCBA → ABCDEF │ 1 2 3 4 5 → 5 4 3 2 1

参见

创建反转范围的副本
(算法函数对象)
以逆序迭代另一个双向 view 元素的视图
(类模板) (范围适配器对象)
反转范围中元素的顺序
(函数模板)