std::rbegin, std::crbegin
来自 cppreference.cn
定义于头文件 <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) (constexpr 始于 C++17) |
template< class C > auto rbegin( const C& c ) -> decltype(c.rbegin()); |
(2) | (始于 C++14) (constexpr 始于 C++17) |
template< class T, std::size_t N > std::reverse_iterator<T*> rbegin( T (&array)[N] ); |
(3) | (始于 C++14) (constexpr 始于 C++17) |
template< class T > std::reverse_iterator<const T*> rbegin( std::initializer_list<T> il ); |
(4) | (始于 C++14) (constexpr 始于 C++17) |
template< class C > auto crbegin( const C& c ) -> decltype(std::rbegin(c)); |
(5) | (始于 C++14) (constexpr 始于 C++17) |
返回给定范围的逆序起始迭代器。
1,2) 返回 c.rbegin(),它通常是表示为 c 的序列的逆序起始迭代器。
5) 返回 std::rbegin(c),其中 c 始终被视为 const 限定。
目录 |
[编辑] 参数
c | - | 具有 rbegin 成员函数的容器或视图 |
array | - | 任意类型的数组 |
il | - | 一个 std::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); // the type of “vi” is std::vector<int>::reverse_iterator std::cout << "*vi = " << *vi << '\n'; *std::rbegin(v) = 42; // OK: after assignment v[2] == 42 // *std::crbegin(v) = 13; // error: the location is read-only int a[] = {-5, 10, 15}; auto ai = std::rbegin(a); // the type of “ai” is std::reverse_iterator<int*> std::cout << "*ai = " << *ai << '\n'; auto il = {3, 1, 4}; // the type of “it” below is 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) |
返回只读范围的逆序迭代器 (自定义点对象) |