std::list<T,Allocator>:: rbegin, std::list<T,Allocator>:: crbegin
From cppreference.net
|
reverse_iterator rbegin
(
)
;
|
(1) |
(自 C++11 起为 noexcept)
(自 C++26 起为 constexpr) |
|
const_reverse_iterator rbegin
(
)
const
;
|
(2) |
(自 C++11 起为 noexcept)
(自 C++26 起为 constexpr) |
|
const_reverse_iterator crbegin
(
)
const
noexcept
;
|
(3) |
(自 C++11 起)
(自 C++26 起为 constexpr) |
返回指向反转后 * this 首元素的反向迭代器。该元素对应于未反转 * this 的最后一个元素。
如果 * this 为空,返回的迭代器等于 rend() 。
目录 |
返回值
指向第一个元素的反向迭代器。
复杂度
常量。
注释
返回的反向迭代器的 底层迭代器 是 end 迭代器 。因此当 end 迭代器失效时,返回的迭代器也会同时失效。
libc++ 向后移植
crbegin()
至 C++98 模式。
示例
运行此代码
#include <algorithm> #include <iostream> #include <numeric> #include <string> #include <list> int main() { std::list<int> nums{1, 2, 4, 8, 16}; std::list<std::string> fruits{"orange", "apple", "raspberry"}; std::list<char> empty; // 打印列表 std::for_each(nums.rbegin(), nums.rend(), [](const int n) { std::cout << n << ' '; }); std::cout << '\n'; // 对列表nums中的所有整数求和(如果有),仅打印结果 std::cout << "Sum of nums: " << std::accumulate(nums.rbegin(), nums.rend(), 0) << '\n'; // 打印列表fruits中的第一个水果,检查是否存在 if (!fruits.empty()) std::cout << "First fruit: " << *fruits.rbegin() << '\n'; if (empty.rbegin() == empty.rend()) std::cout << "list 'empty' is indeed empty.\n"; }
输出:
16 8 4 2 1 Sum of nums: 31 First fruit: raspberry list 'empty' is indeed empty.
参见
|
(C++11)
|
返回指向末尾的反向迭代器
(公开成员函数) |
|
(C++14)
|
返回指向容器或数组起始位置的反向迭代器
(函数模板) |