std::rbegin, std::crbegin_C++中文网

定义于头文件 <iterator>

(1)

template< class C >
auto rbegin( C& c ) -> decltype(c.rbegin());

(C++14 起)
(C++17 前)

template< class C >
constexpr auto rbegin( C& c ) -> decltype(c.rbegin());

(C++17 起)
(1)

template< class C >
auto rbegin( const C& c ) -> decltype(c.rbegin());

(C++14 起)
(C++17 前)

template< class C >
constexpr auto rbegin( const C& c ) -> decltype(c.rbegin());

(C++17 起)
(2)

template< class T, size_t N >
reverse_iterator<T*> rbegin( T (&array)[N] );

(C++14 起)
(C++17 前)

template< class T, size_t N >
constexpr reverse_iterator<T*> rbegin( T (&array)[N] );

(C++17 起)
(3)

template< class C >
auto crbegin( const C& c ) -> decltype(std::rbegin(c));

(C++14 起)
(C++17 前)

template< class C >
constexpr auto crbegin( const C& c ) -> decltype(std::rbegin(c));

(C++17 起)

返回值向给定容器 c 或数组 array 逆向起始的迭代器。

1) 返回指向容器 c 逆向起始的可能有 const 限定的迭代器。

3) 返回指向容器 c 逆向起始的 const 限定的迭代器。

range-rbegin-rend.svg

参数

c - 拥有 rbegin 方法的容器
array - 任意类型的数组

返回值

指向 carray 逆向起始的迭代器

注意

除了包含于 <iterator> ,若包含下列任一头文件,则保证 std::rbeginstd::crbegin 可用: <array><deque><forward_list><list><map><regex><set> <span> (C++20 起)<string> <string_view> (C++17 起)<unordered_map><unordered_set><vector>

重载

可以为未暴露适合的 rbegin() 成员函数的类提供 rbegin 的自定义重载,使之亦能迭代。标准库已经提供了下列重载:

示例

#include <iostream>
#include <vector>
#include <iterator>
 
int main() 
{
    std::vector<int> v = { 3, 1, 4 };
    auto vi = std::rbegin(v);
    std::cout << *vi << '\n'; 
 
    int a[] = { -5, 10, 15 };
    auto ai = std::rbegin(a);
    std::cout << *ai << '\n';
}

输出:

参阅