Namespaces
Variants

std::ranges::subrange<I,S,K>::next

来自 zh.cppreference.net
 
 
范围库
范围适配器
 
 
constexpr subrange next( std::iter_difference_t<I> n = 1 ) const&
    requires std::forward_iterator<I>;
(1) (C++20 起)
constexpr subrange next( std::iter_difference_t<I> n = 1 ) &&;
(2) (C++20 起)

返回一个 subrange,其 begin_ 递增(若 n 为负则递减)。实际的递增(或递减)操作由 advance() 执行。

1) 返回 *this 的副本。
等价于:auto tmp = *this;
tmp.advance(n);
return tmp;
。
2) 返回从 subrange 移动构造的 *this。
等价于:advance(n);
return std::move(*this);
。

参数

n - 迭代器的最大递增次数

返回值

如上所述。

注释

该函数与 advance() 的区别在于后者会原地执行递增(或递减)操作。

示例

#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]

参见

获取迭代器按给定距离递减后的 subrange 副本
(公开成员函数)
按给定距离推进迭代器
(公开成员函数)
(C++11)
递增迭代器
(函数模板)
按给定距离递增迭代器或递增至边界
(算法函数对象)