◐ Shell
clean mode source ↗

std::rend, std::crend - cppreference.com

提供: cppreference.com

<tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody>

ヘッダ <iterator> で定義

(1)

template< class C > auto rend( C& c ) -> decltype(c.rend());

(C++14以上)
(C++17未満)

template< class C > constexpr auto rend( C& c ) -> decltype(c.rend());

(C++17以上)
(1)

template< class C > auto rend( const C& c ) -> decltype(c.rend());

(C++14以上)
(C++17未満)

template< class C > constexpr auto rend( const C& c ) -> decltype(c.rend());

(C++17以上)
(2)

template< class T, size_t N > reverse_iterator<T*> rend( T (&array)[N] );

(C++14以上)
(C++17未満)

template< class T, size_t N > constexpr reverse_iterator<T*> rend( T (&array)[N] );

(C++17以上)
(3)

template< class C > auto crend( const C& c ) -> decltype(std::rend(c));

(C++14以上)
(C++17未満)

template< class C > constexpr auto crend( const C& c ) -> decltype(std::rend(c));

(C++17以上)

指定されたコンテナ c または配列 array の逆の終端を指すイテレータを返します。

1) コンテナ c の逆の終端を指す const 修飾されたまたはされていないイテレータを返します。

2) 配列 array の逆の終端を指す std::reverse_iterator<T*> を返します。

3) コンテナ c の逆の終端を指す const 修飾されたイテレータを返します。

引数

c - メンバ関数 rend を持つコンテナ
array - 任意の型の配列

戻り値

c または array の逆の終端を指すイテレータ。

ノート

<iterator> がインクルードされた場合に加えて <array><deque><forward_list><list><map><regex><set><span> (C++20以上)<string><string_view> (C++17以上)<unordered_map><unordered_set><vector> のいずれかのヘッダがインクルードされた場合も、 std::rend および std::crend が利用可能になることが保証されています。

オーバーロード

適切な rend() メンバ関数を持たないけれどもイテレート可能なクラスに対して、 rend のカスタムオーバーロードを提供しても構いません。 以下のオーバーロードは標準ライブラリによってすでに提供されています。

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    int a[] = {4, 6, -3, 9, 10};
    std::cout << "Array backwards: ";
    std::copy(std::rbegin(a), std::rend(a), std::ostream_iterator<int>(std::cout, " "));

    std::cout << "\nVector backwards: ";
    std::vector<int> v = {4, 6, -3, 9, 10};
    std::copy(std::rbegin(v), std::rend(v), std::ostream_iterator<int>(std::cout, " "));
}

出力:

Array backwards: 10 9 -3 6 4 
Vector backwards: 10 9 -3 6 4

関連項目