Namespaces
Variants

std::rend, std::crend

ja.cppreference.net より
 
 
イテレータライブラリ
イテレータコンセプト
イテレータプリミティブ
アルゴリズムのコンセプトとユーティリティ
間接呼び出し可能コンセプト
共通アルゴリズム要件
(C++20)
(C++20)
(C++20)
ユーティリティ
(C++20)
イテレータアダプタ
レンジアクセス
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
ヘッダ <array>
で定義<deque>
ヘッダ <flat_map>
 (C++23以降) で定義
ヘッダ <flat_set>
 (C++23以降) で定義
ヘッダ <forward_list>
で定義<inplace_vector>
 ヘッダ
(C++26以降) で定義<iterator>
ヘッダ <list>
で定義<map>
ヘッダ <optional>
 (C++26以降) で定義
ヘッダ <regex>
で定義<set>
ヘッダ <span>
 (C++20以降) で定義
ヘッダ <stacktrace>
 (C++23以降) で定義
ヘッダ <string>
で定義<string_view>
 ヘッダ
(C++17以降) で定義<unordered_map>
ヘッダ <unordered_set>
で定義<valarray>
ヘッダ <vector>
template< class C >
auto rend( C& c ) noexcept(noexcept(c.rend()))
    -> decltype(c.rend());
(1) (C++14以降)
(constexpr C++17以降)
template< class C >
auto rend( const C& c ) noexcept(noexcept(c.rend()))
    -> decltype(c.rend());
(2) (C++14以降)
(constexpr C++17以降)
template< class T, std::size_t N >
std::reverse_iterator<T*> rend( T (&array)[N] ) noexcept;
(3) (C++14以降)
(constexpr C++17以降)
template< class T >
std::reverse_iterator<const T*> rend( std::initializer_list<T> il ) noexcept;
(4) (C++14以降)
(constexpr C++17以降)
template< class C >
auto crend( const C& c ) noexcept(noexcept(std::rend(c)))
    -> decltype(std::rend(c));
(5) (C++14以降)
(constexpr C++17以降)

指定された範囲の逆終端へのイテレータを返します。

1,2) c.rend() を返します。これは通常、c で表されるシーケンスの逆終端の1つ後ろを指すイテレータです。
1) C が標準 Container の場合、C::reverse_iterator オブジェクトを返します。
2) C が標準 Container の場合、C::const_reverse_iterator オブジェクトを返します。
3) std::reverse_iterator<T*> の逆終端への array オブジェクトを返します。
4) std::reverse_iterator<const T*> の逆終端への il オブジェクトを返します。
5) std::end(c) を返します。c は常に const 修飾されているものとして扱われます。
C が標準 Container の場合、C::const_reverse_iterator オブジェクトを返します。

パラメータ

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:: rend および std::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

欠陥報告

以下の動作を変更する欠陥報告は、以前に公開されたC++標準に遡及的に適用されました。

DR 適用先 公開時の動作 正しい動作
LWG 3537 C++14 std::rend および std::crend は例外指定を伝播することが必要とされていませんでした 必要

関連項目

(C++11) (C++14)
コンテナまたは配列の終端を指すイテレータを返す
(関数テンプレート)
コンテナまたは配列の先頭を指す逆方向イテレータを返す
(関数テンプレート)
(C++11) (C++14)
コンテナまたは配列の先頭を指すイテレータを返す
(関数テンプレート)
範囲の逆方向終端イテレータを返す
(カスタマイゼーションポイントオブジェクト)
読み取り専用範囲の逆方向終端イテレータを返す
(カスタマイゼーションポイントオブジェクト)