名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | ranges‎ | subrange
 
 
範圍庫 (Ranges library)
範圍介面卡 (Range adaptors)
 
 
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) 返回一個從 *this 移動的 subrange
等效於: 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` 的副本,其迭代器按給定距離遞減
(public member function) [編輯]
將迭代器按給定距離前進
(public member function) [編輯]
(C++11)
遞增迭代器
(function template) [編輯]
按給定距離或到邊界遞增迭代器
(algorithm function object)[編輯]