std::deque<T,Allocator>::rbegin, std::deque<T,Allocator>::crbegin

從 cppreference.com
< cpp‎ | container‎ | deque
 
 
 
 
reverse_iterator rbegin();
(1) (C++11 起為 noexcept)
(C++26 起為 constexpr)
const_reverse_iterator rbegin() const;
(2) (C++11 起為 noexcept)
(C++26 起為 constexpr)
const_reverse_iterator crbegin() const noexcept;
(3) (C++11 起)
(C++26 起為 constexpr)

返回指向逆向的 *this 的首元素的逆向迭代器。它對應非逆向 *this 的末元素。

如果 *this 為空,那麼返回的迭代器等於 rend()

range-rbegin-rend.svg

目錄

[編輯] 返回值

指向首元素的逆向迭代器。

[編輯] 複雜度

常數。

[編輯] 註解

返回的逆向迭代器的底層迭代器尾迭代器。因此在尾迭代器失效時,返回的迭代器也會失效。

libc++ 將 crbegin() 向後移植到 C++98 模式。

[編輯] 示例

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <deque>
 
int main()
{
    std::deque<int> nums{1, 2, 4, 8, 16};
    std::deque<std::string> fruits{"orange", "apple", "raspberry"};
    std::deque<char> empty;
 
    // 打印 deque。
    std::for_each(nums.rbegin(), nums.rend(), [](const int n) { std::cout << n << ' '; });
    std::cout << '\n';
 
    // 求和 deque nums 中的所有整数(若存在),仅打印结果。
    std::cout << "求和 nums: "
              << std::accumulate(nums.rbegin(), nums.rend(), 0) << '\n';
 
    // 打印 deque fruits 中的第一个水果,不检查是否有一个。
    if (!fruits.empty())
        std::cout << "第一个水果: " << *fruits.rbegin() << '\n';
 
    if (empty.rbegin() == empty.rend())
        std::cout << "deque 'empty' 确实是空的。\n";
}

輸出:

16 8 4 2 1
求和 nums: 31
第一个水果: raspberry
deque 'empty' 确实是空的。

[編輯] 參閱

(C++11)
返回指向末尾的逆向迭代器
(公開成員函數) [編輯]
返回指向一個容器或數組的逆向迭代器
(函數模板) [編輯]