std::rend, std::crend - cppreference.com
来自cppreference.com
| 在标头 |
||
| 在标头 |
||
| 在标头 |
(C++23 起) |
|
| 在标头 |
(C++23 起) |
|
| 在标头 |
||
| 在标头 |
(C++26 起) |
|
| 在标头 |
||
| 在标头 |
||
| 在标头 |
||
| 在标头 |
(C++26 起) |
|
| 在标头 |
||
| 在标头 |
||
| 在标头 |
(C++20 起) |
|
| 在标头 |
(C++23 起) |
|
| 在标头 |
||
| 在标头 |
(C++17 起) |
|
| 在标头 |
||
| 在标头 |
||
| 在标头 |
||
| 在标头 |
||
template< class C > auto rend( C& c ) noexcept(noexcept(c.rend())) -> decltype(c.rend()); |
(1) | (C++14 起) (C++17 起为 constexpr) |
template< class C > auto rend( const C& c ) noexcept(noexcept(c.rend())) -> decltype(c.rend()); |
(2) | (C++14 起) (C++17 起为 constexpr) |
template< class T, std::size_t N > std::reverse_iterator<T*> rend( T (&array)[N] ) noexcept; |
(3) | (C++14 起) (C++17 起为 constexpr) |
template< class T > std::reverse_iterator<const T*> rend( std::initializer_list<T> il ) noexcept; |
(4) | (C++14 起) (C++17 起为 constexpr) |
template< class C > auto crend( const C& c ) noexcept(noexcept(std::rend(c))) -> decltype(std::rend(c)); |
(5) | (C++14 起) (C++17 起为 constexpr) |
返回值向给定范围的逆向结尾的迭代器。
1,2) 返回 c.rend(),通常是指向 c 所代表的逆序序列末尾后一位置的迭代器。
1) 如果 C 是标准容器 (Container) ,那么就会返回 C::reverse_iterator 对象。
2) 如果 C 是标准容器 (Container) ,那么就会返回 C::const_reverse_iterator 对象。
3) 返回指向数组 array 的逆向结尾的 std::reverse_iterator<T*> 对象。
4) 返回指向 il 的逆向结尾的 std::reverse_iterator<const T*> 对象。
5) 返回 std::rend(c),这里 c 始终当做 const 限定。
如果 C 是标准容器 (Container) ,那么就会返回 C::const_reverse_iterator 对象。
参数
| c | - | 拥有 rend 成员函数的容器或视图
|
| array | - | 任意类型的数组 |
| il | - | std::initializer_list
|
返回值
1,2) c.rend()
3) std::reverse_iterator<T*>(array)
4) std::reverse_iterator<const T*>(il.begin())
5) std::rend(c)
异常
1,2) 底层 c.rend() 调用抛出时所抛的异常。
5) 底层 std::rend(c) 调用抛出时所抛的异常。
重载
可对未暴露适合的 rend() 成员函数的类或枚举提供 rend 的定制重载,从而能迭代它们。
|
实参依赖查找找到的 |
(C++20 起) |
注解
需要针对 std::initializer_list 的重载,因为它没有成员函数 rend。
示例
#include <algorithm> #include <iostream> #include <iterator> #include <vector> int main() { int a[]{4, 6, -3, 9, 10}; std::cout << "C 风格数组 `a` 逆序:"; std::copy(std::rbegin(a), std::rend(a), std::ostream_iterator<int>(std::cout, " ")); auto il = {3, 1, 4}; std::cout << "\nstd::initializer_list `il` 逆序:"; std::copy(std::rbegin(il), std::rend(il), std::ostream_iterator<int>(std::cout, " ")); std::vector<int> v{4, 6, -3, 9, 10}; std::cout << "\nstd::vector `v` 逆序:"; std::copy(std::rbegin(v), std::rend(v), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; }
输出:
C 风格数组 `a` 逆序:10 9 -3 6 4 std::initializer_list `il` 逆序:4 1 3 std::vector `v` 逆序:10 9 -3 6 4
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 3537 | C++14 | 未要求 std::rend 与 std::crend 传播异常说明
|
已要求 |