std::vector<T,Allocator>::rend, std::vector<T,Allocator>::crend
来自 zh.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++ 向后移植
crend()
至 C++98 模式。
示例
运行此代码
#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)
|
返回指向起始的反向迭代器
(公开成员函数) |
|
(C++14)
|
返回容器或数组的逆向末尾迭代器
(函数模板) |