Namespaces
Variants

std::deque<T,Allocator>:: rend, std::deque<T,Allocator>:: crend

From ja.cppreference.net

reverse_iterator rend ( ) ;
(1) (C++11以降 noexcept)
(C++26以降 constexpr)
const_reverse_iterator rend ( ) const ;
(2) (C++11以降 noexcept)
(C++26以降 constexpr)
const_reverse_iterator crend ( ) const noexcept ;
(3) (C++11以降)
(C++26以降 constexpr)

反転された * this の最後の要素の次を指す逆方向イテレータを返します。これは、非反転の * this の最初の要素の前の要素に対応します。

この返されたイテレータはセンチネルとしてのみ機能します。これが dereferenceable であることは保証されません。

range-rbegin-rend.svg

目次

戻り値

最後の要素の次の要素への逆方向イテレータ。

計算量

定数。

注記

libc++はC++98モードに crend() をバックポートします。

例

#include <algorithm>
#include <cassert>
#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;

    // Print deque nums.
    std::for_each(nums.crbegin(), nums.crend(),
                  [](const int n) { std::cout << n << ' '; });
    std::cout << '\n';

    // Sum all integers in the deque nums, printing only the result.
    std::cout << "Sum of nums: "
              << std::accumulate(nums.crbegin(), nums.crend(), 0) << '\n';

    // Print the last fruit in the deque fruits, checking if there is any.
    if (!fruits.empty())
        std::cout << "Last fruit: " << *fruits.crbegin() << '\n';

    if (empty.crbegin() == empty.crend())
        std::cout << "deque ‘empty’ is indeed empty.\n";

    // Modify the last element in nums.
    *nums.rbegin() = 32;
    assert(*nums.crbegin() == 32);
}

出力:

16 8 4 2 1
Sum of nums: 31
Last fruit: raspberry
deque ‘empty’ is indeed empty.

関連項目

先頭を指す逆方向イテレータを返す
(公開メンバ関数)
(C++14)
コンテナまたは配列の逆方向終端イテレータを返す
(関数テンプレート)