C++ Array 库 - rend() 函数
描述
C++ 函数 std::array::rend() 返回一个反向迭代器,它指向数组第一个元素之前的理论元素。
该区域以相反的顺序表示存在于 array::rbegin() 和 array::rend() 范围之间的所有数组元素。
声明
以下是 std::array::rend() 函数形式 std::array 头的声明。
reverse_iterator rend() noexcept; const_reverse_iterator rend() noexcept;
参数
None
返回值
返回一个反向迭代器,它指向数组第一个元素之前的理论元素。 换句话说,它返回一个反向迭代器,它指向反向数组容器的最后一个元素。
如果数组对象是 const 限定的,则方法返回 const 反向迭代器,否则返回反向迭代器。
异常
此成员函数从不抛出异常。
时间复杂度
常数,即 O(1)
示例
以下示例显示了 std::array::rend() 函数的用法。
#include <iostream>
#include <array>
using namespace std;
int main(void) {
array<int, 5> arr = {10, 20, 30, 40, 50};
/* reverse iterator points to the last element of the array */
auto s = arr.rbegin();
/* reverse iterator points to the first element of the array */
auto e = arr.rend();
/* iterate array in reverse order */
while (s < e) {
cout << *s << " ";
++s;
}
cout << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
50 40 30 20 10

