std::ranges::subrange<I,S,K>::prev
来自 cppreference.cn
constexpr subrange prev( std::iter_difference_t<I> n = 1 ) const requires std::bidirectional_iterator<I>; |
(自 C++20 起) | |
返回 begin_
被递减(如果 n 为负则递增)的 *this 的副本。实际的递减(或递增)操作由 advance()
执行。
等效于:auto tmp = *this;
tmp.advance(-n);
return tmp;。
内容 |
[编辑] 参数
n | - | 迭代器递减的次数 |
[编辑] 返回值
如上所述。
[编辑] 注解
此函数与 advance()
之间的区别在于后者执行原地递减(或递增)。
[编辑] 示例
运行此代码
#include <iterator> #include <list> #include <print> #include <ranges> int main() { std::list list{1, 2, 3, 4, 5}; std::ranges::subrange sub{std::next(list.begin(), 2), std::prev(list.end(), 2)}; std::println("{} {} {}", sub, sub.prev(), sub.prev(2)); }
输出
[3] [2, 3] [1, 2, 3]
[编辑] 参见
获得 subrange 的副本,其迭代器按给定距离前进(公共成员函数) | |
将迭代器按给定距离前进 (公共成员函数) | |
(C++11) |
递减迭代器 (函数模板) |
(C++20) |
将迭代器按给定距离或到边界递减 (算法函数对象) |