跳到主要内容

std::deque rend() 方法

// Non const version
iterator rend() noexcept;

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

返回一个反向迭代器

指向反向 deque 的最后一个元素。
它对应于非反向 deque 的第一个元素**之前**的元素。

它有效地返回一个指向原始 deque 开头之前的迭代器。

危险

尝试解引用“开头之前”的迭代器是未定义行为

.

注意

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

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

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

参数

(无)

返回值

反向迭代器指向最后一个元素之后的元素。

复杂度

常数。

rend 和 crend 的区别

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

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

#include <deque>

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

示例

Main.cpp
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <deque>

int main()
{
std::deque<int> nums {1, 2, 4, 8, 16};
std::deque<std::string> fruits {"orange", "apple", "raspberry"};
std::deque<char> empty;

// Print deque.
std::for_each(nums.rbegin(), nums.rend(), [](const int n) { std::cout << n << ' '; });
std::cout << '\n';

// Sums all integers in the deque nums (if any), printing only the result.
std::cout << "Sum of nums: "
<< std::accumulate(nums.rbegin(), nums.rend(), 0) << '\n';

// Prints the first fruit in the deque fruits, checking if there is any.
if (!fruits.empty())
std::cout << "First fruit: " << *fruits.rbegin() << '\n';

if (empty.rbegin() == empty.rend())
std::cout << "deque 'empty' is indeed empty.\n";
}
输出
16 8 4 2 1
Sum of nums: 31
First fruit: raspberry
deque 'empty' is indeed empty.
本文档源自 此 CppReference 页面。它可能已被修改以进行改进或满足编辑偏好。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::deque rend() 方法

// Non const version
iterator rend() noexcept;

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

返回一个反向迭代器

指向反向 deque 的最后一个元素。
它对应于非反向 deque 的第一个元素**之前**的元素。

它有效地返回一个指向原始 deque 开头之前的迭代器。

危险

尝试解引用“开头之前”的迭代器是未定义行为

.

注意

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

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

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

参数

(无)

返回值

反向迭代器指向最后一个元素之后的元素。

复杂度

常数。

rend 和 crend 的区别

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

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

#include <deque>

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

示例

Main.cpp
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <deque>

int main()
{
std::deque<int> nums {1, 2, 4, 8, 16};
std::deque<std::string> fruits {"orange", "apple", "raspberry"};
std::deque<char> empty;

// Print deque.
std::for_each(nums.rbegin(), nums.rend(), [](const int n) { std::cout << n << ' '; });
std::cout << '\n';

// Sums all integers in the deque nums (if any), printing only the result.
std::cout << "Sum of nums: "
<< std::accumulate(nums.rbegin(), nums.rend(), 0) << '\n';

// Prints the first fruit in the deque fruits, checking if there is any.
if (!fruits.empty())
std::cout << "First fruit: " << *fruits.rbegin() << '\n';

if (empty.rbegin() == empty.rend())
std::cout << "deque 'empty' is indeed empty.\n";
}
输出
16 8 4 2 1
Sum of nums: 31
First fruit: raspberry
deque 'empty' is indeed empty.
本文档源自 此 CppReference 页面。它可能已被修改以进行改进或满足编辑偏好。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。