std::vector<T,Allocator>:: rend, std::vector<T,Allocator>:: crend
From cppreference.net
|
reverse_iterator rend
(
)
;
|
(1) |
(C++11以降 noexcept)
(C++20以降 constexpr) |
|
const_reverse_iterator rend
(
)
const
;
|
(2) |
(C++11以降 noexcept)
(C++20以降 constexpr) |
|
const_reverse_iterator crend
(
)
const
noexcept
;
|
(3) |
(C++11以降)
(C++20以降 constexpr) |
反転された * this の最後の要素の次を指す逆方向イテレータを返します。これは非反転 * this の最初の要素の前の要素に対応します。
この返されたイテレータはセンチネルとしてのみ機能します。これが デリファレンス可能 であることは保証されません。
目次 |
戻り値
最後の要素の次の要素への逆イテレータ。
計算量
定数。
注記
libc++はC++98モードに
crend()
をバックポートします。
例
このコードを実行
#include <algorithm> #include <iostream> #include <numeric> #include <string> #include <vector> int main() { std::vector<int> nums{1, 2, 4, 8, 16}; std::vector<std::string> fruits{"orange", "apple", "raspberry"}; std::vector<char> empty; // ベクトルを出力 std::for_each(nums.rbegin(), nums.rend(), [](const int n) { std::cout << n << ' '; }); std::cout << '\n'; // ベクトルnums内の全整数を合計し(存在する場合)、結果のみを出力 std::cout << "Sum of nums: " << std::accumulate(nums.rbegin(), nums.rend(), 0) << '\n'; // ベクトルfruitsの最初のフルーツを出力(存在確認付き) if (!fruits.empty()) std::cout << "First fruit: " << *fruits.rbegin() << '\n'; if (empty.rbegin() == empty.rend()) std::cout << "vector 'empty' is indeed empty.\n"; }
出力:
16 8 4 2 1 Sum of nums: 31 First fruit: raspberry vector 'empty' is indeed empty.
関連項目
|
(C++11)
|
先頭を指す逆方向イテレータを返す
(public member function) |
|
(C++14)
|
コンテナまたは配列の終端を指す逆方向イテレータを返す
(function template) |