std::rend, std::crend
來自 cppreference.com
< cpp | 迭代器 (iterator)
定義於標頭檔案 <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 rend( C& c ) -> decltype(c.rend()); |
(1) | (C++14 起) (自 C++17 起為 constexpr) |
template< class C > auto rend( const C& c ) -> decltype(c.rend()); |
(2) | (C++14 起) (自 C++17 起為 constexpr) |
template< class T, std::size_t N > std::reverse_iterator<T*> rend( T (&array)[N] ); |
(3) | (C++14 起) (自 C++17 起為 constexpr) |
template< class T > std::reverse_iterator<const T*> rend( std::initializer_list<T> il ); |
(4) | (C++14 起) (自 C++17 起為 constexpr) |
template< class C > auto crend( const C& c ) -> decltype(std::rend(c)); |
(5) | (C++14 起) (自 C++17 起為 constexpr) |
返回給定範圍的反向末尾迭代器。
1,2) 返回 c.rend(),它通常是表示 c 的序列的反向末尾之後的一個迭代器。
目錄 |
[編輯] 引數
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) c.rend()
[編輯] 異常
可能丟擲實現定義的異常。
[編輯] 過載
可以為不暴露合適 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-style array `a` backwards: "; 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` backwards: "; 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` backwards: "; std::copy(std::rbegin(v), std::rend(v), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; }
輸出
C-style array `a` backwards: 10 9 -3 6 4 std::initializer_list `il` backwards: 4 1 3 std::vector `v` backwards: 10 9 -3 6 4
[編輯] 參閱
(C++11)(C++14) |
返回指向容器或陣列末尾的迭代器 (函式模板) |
返回指向容器或陣列開頭的反向迭代器 (函式模板) | |
(C++11)(C++14) |
返回指向容器或陣列開頭的迭代器 (函式模板) |
(C++20) |
返回指向範圍的反向結束迭代器 (自定義點物件) |
(C++20) |
返回只讀範圍的反向結束迭代器 (自定義點物件) |