std::ranges::subrange<I,S,K>::next
来自 cppreference.cn
constexpr subrange next( std::iter_difference_t<I> n = 1 ) const& requires std::forward_iterator<I>; |
(1) | (since C++20) |
constexpr subrange next( std::iter_difference_t<I> n = 1 ) &&; |
(2) | (since C++20) |
返回一个 subrange
,其 begin_
被递增(如果 n 为负数则递减)。实际的递增(或递减)操作由 advance()
执行。
1) 返回
*this
的副本。 等价于: auto tmp = *this;
tmp.advance(n);
return tmp;。
tmp.advance(n);
return tmp;。
2) 返回从
*this
移动的 subrange
。 等价于: advance(n);
return std::move(*this);。
return std::move(*this);。
目录 |
[edit] 参数
n | - | 迭代器的最大递增次数 |
[edit] 返回值
如上所述。
[edit] 注释
此函数与 advance()
的区别在于后者执行就地递增(或递减)。
[edit] 示例
运行此代码
#include <array> #include <iterator> #include <print> #include <ranges> int main() { std::array arr{1, 2, 3, 4, 5, 6, 7}; std::ranges::subrange sub{std::next(arr.begin(), 2), std::prev(arr.end(), 2)}; std::println("1) sub: {}", sub); std::println("2) sub: {}", sub.next()); std::println("3) sub: {}", sub.next(2)); }
输出
1) sub: [3, 4, 5] 2) sub: [4, 5] 3) sub: [5]
[edit] 参见
获取 subrange 的副本,其迭代器按给定距离递减 (public member function) | |
按给定距离递增迭代器 (public member function) | |
(C++11) |
递增迭代器 (function template) |
(C++20) |
按给定距离或到边界递增迭代器 (algorithm function object) |