std::rbegin, std::crbegin
来自cppreference.com
<tbody>
{{dcl rev multi|num=3
</tbody>
| 在标头 <array> 定义
|
||
| 在标头 <deque> 定义
|
||
| 在标头 <flat_map> 定义
|
||
| 在标头 <flat_set> 定义
|
||
| 在标头 <forward_list> 定义
|
||
| 在标头 <inplace_vector> 定义
|
||
| 在标头 <iterator> 定义
|
||
| 在标头 <list> 定义
|
||
| 在标头 <map> 定义
|
||
| 在标头 <regex> 定义
|
||
| 在标头 <set> 定义
|
||
| 在标头 <span> 定义
|
||
| 在标头 <string> 定义
|
||
| 在标头 <string_view> 定义
|
||
| 在标头 <unordered_map> 定义
|
||
| 在标头 <unordered_set> 定义
|
||
| 在标头 <vector> 定义
|
||
template< class C > auto rbegin( C& c ) -> decltype(c.rbegin()); |
(1) | (C++14 起) (C++17 起为 constexpr) |
template< class C > auto rbegin( const C& c ) -> decltype(c.rbegin()); |
(2) | (C++14 起) (C++17 起为 constexpr) |
template< class T, std::size_t N > std::reverse_iterator<T*> rbegin( T (&array)[N] ); |
(3) | (C++14 起) (C++17 起为 constexpr) |
template< class T > std::reverse_iterator<const T*> rbegin( std::initializer_list<T> il ); |
(4) | (C++14 起) (C++17 起为 constexpr) |
template< class C > auto crbegin( const C& c ) -> decltype(std::rbegin(c)); |
(5) | (C++14 起) (C++17 起为 constexpr) |
返回指向给定范围的逆向起始的迭代器。
1,2) 返回
c.rbegin(),它通常是指向 c 所代表的序列逆向起始的迭代器。3) 返回指向
array 的逆向起始的 std::reverse_iterator<T*> 对象。4) 返回指向
il 的逆向起始的 std::reverse_iterator<const T*> 对象。5) 返回
std::begin(c),这里 c 始终被视为 const 限定。参数
| c | - | 拥有 rbegin 方法的容器或视图
|
| array | - | 任意类型的数组 |
| il | - | initializer_list
|
返回值
1,2)
c.rbegin()3)
std::reverse_iterator<T*>(array + N)4)
std::reverse_iterator<const T*>(il.end())5)
c.rbegin()异常
可能会抛出由实现定义的异常。
重载
可以为未暴露适合的 rbegin() 成员函数的类或枚举提供 rbegin 的自定义重载,从而能迭代它。
|
实参依赖查找找到的 |
(C++20 起) |
注解
需要针对 std::initializer_list 的重载,因为它没有成员函数 rbegin。
示例
运行此代码
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> v = {3, 1, 4};
auto vi = std::rbegin(v); // “vi” 的类型是 std::vector<int>::reverse_iterator
std::cout << "*vi = " << *vi << '\n';
*std::rbegin(v) = 42; // OK:赋值后 v[2] == 42
// *std::crbegin(v) = 13; // 错误:此位置只读
int a[] = {-5, 10, 15};
auto ai = std::rbegin(a); // “ai” 的类型是 std::reverse_iterator<int*>
std::cout << "*ai = " << *ai << '\n';
auto il = {3, 1, 4};
// 下面的 “it” 的类型是 std::reverse_iterator<int const*>:
for (auto it = std::rbegin(il); it != std::rend(il); ++it)
std::cout << *it << ' ';
std::cout << '\n';
}
输出:
*vi = 4
*ai = 15
4 1 3
参阅
(C++11)(C++14) |
返回指向容器或数组起始的迭代器 (函数模板) |
(C++11)(C++14) |
返回指向容器或数组结尾的迭代器 (函数模板) |
(C++14) |
返回容器或数组的逆向尾迭代器 (函数模板) |
(C++20) |
返回指向范围的逆向迭代器 (定制点对象) |
(C++20) |
返回指向只读范围的逆向迭代器 (定制点对象) |