名稱空間
變體
操作

std::rbegin, std::crbegin

來自 cppreference.com
 
 
迭代器庫
迭代器概念
迭代器原語
演算法概念與工具
間接可呼叫概念
常用演算法要求
工具
迭代器介面卡
範圍訪問
rbegin (反向開始)crbegin (常量反向開始)
(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 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 的序列的反向起始迭代器。
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::rbegin(c),其中 c 總是被視為 const-qualified (常量限定)。
C 是標準容器,則返回 C::const_reverse_iterator 物件。

range-rbegin-rend.svg

目錄

[編輯] 引數

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

透過實參依賴查詢找到的 rbegin 過載可用於定製 std::ranges::rbeginstd::ranges::crbegin 的行為。

(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

[編輯] 參閱

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