跳到主要内容

std::array rbegin()/crbegin() 方法

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

// Const version
constexpr const_iterator rbegin() const noexcept;
constexpr const_iterator crbegin() const noexcept;

返回一个反向迭代器

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

注意

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

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

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

参数

(无)

返回值

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

复杂度

常数。

rbegin 和 crbegin 之间的区别

对于 const 容器 c,rbegin 和 crbegin 相同 - c.rbegin() == c.crbegin()

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

#include <array>

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

示例

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

int main()
{
constexpr std::array<std::string_view, 8> data = {"▁","▂","▃","▄","▅","▆","▇","█"};

std::array<std::string, std::size(data)> arr;

std::copy(data.cbegin(), data.cend(), arr.begin());
// ^ ^ ^

auto print = [](const std::string_view s) { std::cout << s << ' '; };

print("Print 'arr' in direct order using [cbegin, cend):\t");
std::for_each(arr.cbegin(), arr.cend(), print);
// ^ ^
print("\n\nPrint 'arr' in reverse order using [crbegin, crend):\t");
std::for_each(arr.crbegin(), arr.crend(), print);
// ^^ ^^
print("\n");
}
可能输出
Print 'arr' in direct order using [cbegin, cend):        ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ 

Print 'arr' in reverse order using [crbegin, crend): █ ▇ ▆ ▅ ▄ ▃ ▂ ▁
本文档源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”可查看对本文档进行的所有更改。
悬停查看原始许可证。

std::array rbegin()/crbegin() 方法

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

// Const version
constexpr const_iterator rbegin() const noexcept;
constexpr const_iterator crbegin() const noexcept;

返回一个反向迭代器

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

注意

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

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

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

参数

(无)

返回值

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

复杂度

常数。

rbegin 和 crbegin 之间的区别

对于 const 容器 c,rbegin 和 crbegin 相同 - c.rbegin() == c.crbegin()

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

#include <array>

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

示例

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

int main()
{
constexpr std::array<std::string_view, 8> data = {"▁","▂","▃","▄","▅","▆","▇","█"};

std::array<std::string, std::size(data)> arr;

std::copy(data.cbegin(), data.cend(), arr.begin());
// ^ ^ ^

auto print = [](const std::string_view s) { std::cout << s << ' '; };

print("Print 'arr' in direct order using [cbegin, cend):\t");
std::for_each(arr.cbegin(), arr.cend(), print);
// ^ ^
print("\n\nPrint 'arr' in reverse order using [crbegin, crend):\t");
std::for_each(arr.crbegin(), arr.crend(), print);
// ^^ ^^
print("\n");
}
可能输出
Print 'arr' in direct order using [cbegin, cend):        ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ 

Print 'arr' in reverse order using [crbegin, crend): █ ▇ ▆ ▅ ▄ ▃ ▂ ▁
本文档源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”可查看对本文档进行的所有更改。
悬停查看原始许可证。