跳到主要内容

std::vector rend() 方法

// Non const version
reverse_iterator rend() noexcept;

// Const version
reverse_iterator rend() const noexcept;

// Const version
const_reverse_iterator crend() const noexcept;

它对应于未反转向量的最后一个元素。

返回一个反向迭代器

它对应于反转向量的最后一个元素。它对应于未反转向量的第一个元素<强>之前的元素。

它实际上返回一个指向原始向量末尾之后的迭代器。尝试解引用末尾之后的迭代器是未定义行为

.

注意

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

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

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

参数

(无)

返回值

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

复杂度

常数。

rend 和 crend 的区别

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

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

#include <map> 

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

示例

Main.cpp
#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;

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

// Sums all integers in the vector 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 vector fruits, checking if there is any.
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.
本文来源于 此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::vector rend() 方法

// Non const version
reverse_iterator rend() noexcept;

// Const version
reverse_iterator rend() const noexcept;

// Const version
const_reverse_iterator crend() const noexcept;

它对应于未反转向量的最后一个元素。

返回一个反向迭代器

它对应于反转向量的最后一个元素。它对应于未反转向量的第一个元素<强>之前的元素。

它实际上返回一个指向原始向量末尾之后的迭代器。尝试解引用末尾之后的迭代器是未定义行为

.

注意

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

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

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

参数

(无)

返回值

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

复杂度

常数。

rend 和 crend 的区别

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

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

#include <map> 

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

示例

Main.cpp
#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;

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

// Sums all integers in the vector 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 vector fruits, checking if there is any.
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.
本文来源于 此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。