名稱空間
變體
操作

std::rend, std::crend

來自 cppreference.com
 
 
迭代器庫
迭代器概念
迭代器原語
演算法概念與工具
間接可呼叫概念
常用演算法要求
工具
迭代器介面卡
範圍訪問
rend (反向結束)crend (常量反向結束)
(C++14)(C++14)  
 
定義於標頭檔案 <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 的序列的反向末尾之後的一個迭代器。
1) 如果 C 是標準容器,返回一個 C::reverse_iterator 物件。
2) 如果 C 是標準容器,返回一個 C::const_reverse_iterator 物件。
3) 返回一個 std::reverse_iterator<T*> 物件,指向 array 的反向末尾。
4) 返回一個 std::reverse_iterator<const T*> 物件,指向 il 的反向末尾。
5) 返回 std::end(c),其中 c 始終被視為 const-qualified (常量限定)。
如果 C 是標準容器,返回一個 C::const_reverse_iterator 物件。

range-rbegin-rend.svg

目錄

[編輯] 引數

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 的自定義過載。

透過實參依賴查詢找到的 rend 過載可用於自定義 std::ranges::rendstd::ranges::crend 的行為。

(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

[編輯] 參閱

返回指向容器或陣列末尾的迭代器
(函式模板) [編輯]
返回指向容器或陣列開頭的反向迭代器
(函式模板) [編輯]
返回指向容器或陣列開頭的迭代器
(函式模板) [編輯]
返回指向範圍的反向結束迭代器
(自定義點物件)[編輯]
返回只讀範圍的反向結束迭代器
(自定義點物件)[編輯]