跳到主要内容

std::array rend()/crend() 方法

// Non-const version
constexpr iterator rend() noexcept;

// Const version
constexpr const_iterator rend() const noexcept;
constexpr const_iterator crend() const noexcept;

它对应于非反转数组的最后一个元素。

返回一个反向迭代器

对应于反转数组的最后一个元素。它对应于非反转数组的第一个元素**之前**的元素。

它有效地返回一个指向原始数组末尾之外的迭代器。尝试解引用末尾之外的迭代器是未定义行为

.

注意

此方法实际上不会反转数组,它只是返回一个迭代器,该迭代器指向数组第一个元素之前的元素,并且其 +---++ 运算符的实现略有改变。

例如,it++会递减内部指针,而it--会递增内部指针(以便以相反的顺序遍历容器实际工作)。

如果容器为空,则返回的迭代器将等于 rbegin()

参数

(无)

返回值

反向迭代器指向第一个元素之前的元素。

复杂度

常数。

为什么是第一个元素之前?

重要

本节需要改进。您可以通过编辑此文档页面来帮助我们。

rend 和 crend 的区别

对于常量容器c,rend 和 crend 是相同的 - c.rend() == c.crend()

对于非常量类型c的容器,它们返回不同的迭代器

#include <array> 

int main()
{
std::array<int, 5> arr = {1, 2, 3, 4, 5};
auto it = arr.rend(); // Type: std::array<int, 5>::reverse_iterator
*std::prev(it) = 5; // ✔ Ok
}

示例

Main.cpp
#include <algorithm>
#include <array>
#include <iostream>

int main()
{
std::array<int, 11> a {1, 11, 11, 35, 0, 12, 79, 76, 76, 69, 40};

// print elements of array in reverse order using const_reverse_iterator`s
std::for_each(a.crbegin(), a.crend(), [](int e){ std::cout << e << ' '; });
// ^^ ^^
std::cout << '\n';

// modify each element of array using non-const reverse_iterator`s
std::for_each(a.rbegin(), a.rend(), [](int& e){ e += 32; });
// ^ ^ ^

// print elements as chars in reverse order using const_reverse_iterator`s
std::for_each(a.crbegin(), a.crend(), [](char e){ std::cout << e; });
// ^^ ^^ ^^^^
std::cout << '\n';
}
输出
40 69 76 76 79 12 0 35 11 11 1 
Hello, C++!
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。单击“编辑此页面”以查看此文档的所有更改。
悬停查看原始许可证。

std::array rend()/crend() 方法

// Non-const version
constexpr iterator rend() noexcept;

// Const version
constexpr const_iterator rend() const noexcept;
constexpr const_iterator crend() const noexcept;

它对应于非反转数组的最后一个元素。

返回一个反向迭代器

对应于反转数组的最后一个元素。它对应于非反转数组的第一个元素**之前**的元素。

它有效地返回一个指向原始数组末尾之外的迭代器。尝试解引用末尾之外的迭代器是未定义行为

.

注意

此方法实际上不会反转数组,它只是返回一个迭代器,该迭代器指向数组第一个元素之前的元素,并且其 +---++ 运算符的实现略有改变。

例如,it++会递减内部指针,而it--会递增内部指针(以便以相反的顺序遍历容器实际工作)。

如果容器为空,则返回的迭代器将等于 rbegin()

参数

(无)

返回值

反向迭代器指向第一个元素之前的元素。

复杂度

常数。

为什么是第一个元素之前?

重要

本节需要改进。您可以通过编辑此文档页面来帮助我们。

rend 和 crend 的区别

对于常量容器c,rend 和 crend 是相同的 - c.rend() == c.crend()

对于非常量类型c的容器,它们返回不同的迭代器

#include <array> 

int main()
{
std::array<int, 5> arr = {1, 2, 3, 4, 5};
auto it = arr.rend(); // Type: std::array<int, 5>::reverse_iterator
*std::prev(it) = 5; // ✔ Ok
}

示例

Main.cpp
#include <algorithm>
#include <array>
#include <iostream>

int main()
{
std::array<int, 11> a {1, 11, 11, 35, 0, 12, 79, 76, 76, 69, 40};

// print elements of array in reverse order using const_reverse_iterator`s
std::for_each(a.crbegin(), a.crend(), [](int e){ std::cout << e << ' '; });
// ^^ ^^
std::cout << '\n';

// modify each element of array using non-const reverse_iterator`s
std::for_each(a.rbegin(), a.rend(), [](int& e){ e += 32; });
// ^ ^ ^

// print elements as chars in reverse order using const_reverse_iterator`s
std::for_each(a.crbegin(), a.crend(), [](char e){ std::cout << e; });
// ^^ ^^ ^^^^
std::cout << '\n';
}
输出
40 69 76 76 79 12 0 35 11 11 1 
Hello, C++!
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。单击“编辑此页面”以查看此文档的所有更改。
悬停查看原始许可证。