std::vector<T,Allocator>::rend, std::vector<T,Allocator>::crend
ja.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) |
逆順の vector の最後の要素の次の要素を指す逆イテレータを返します。これは、元の順序の vector の最初の要素の前の要素に対応します。この要素はプレースホルダとして機能し、それにアクセスしようとすると未定義動作になります。
戻り値
最後の要素の次の要素への逆イテレータ。
計算量
定数。
注記
libc++はC++98モードに
crend()
をバックポートします。
例
このコードを実行
#include <algorithm>
#include <cassert>
#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;
// Print vector nums.
std::for_each(nums.crbegin(), nums.crend(),
[](const int n) { std::cout << n << ' '; });
std::cout << '\n';
// Sum all integers in the vector nums, printing only the result.
std::cout << "Sum of nums: "
<< std::accumulate(nums.crbegin(), nums.crend(), 0) << '\n';
// Print the last fruit in the vector fruits, checking if there is any.
if (!fruits.empty())
std::cout << "Last fruit: " << *fruits.crbegin() << '\n';
if (empty.crbegin() == empty.crend())
std::cout << "vector ‘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
vector ‘empty’ is indeed empty.
関連項目
|
(C++11)
|
先頭を指す逆方向イテレータを返す
(public member function) |
|
(C++14)
|
コンテナまたは配列の終端を指す逆方向イテレータを返す
(function template) |