最終更新日時(UTC):
が更新

履歴 編集

function template
<iterator>

std::ranges::next(C++20)

namespace std::ranges {
  template<input_or_output_iterator I>
  constexpr I next(I x);                                    // (1)

  template<input_or_output_iterator I>
  constexpr I next(I x, iter_difference_t<I> n);            // (2)

  template<input_or_output_iterator I, sentinel_for<I> S>
  constexpr I next(I x, S bound);                           // (3)

  template<input_or_output_iterator I, sentinel_for<I> S>
  constexpr I next(I x, iter_difference_t<I> n, S bound);   // (4)
}

概要

n回あるいは指定された位置まで前方に進めたイテレータを返す。

ranges::advance()と違い、引数として渡されたイテレータへの参照を書き換えるのではなく、n回進んだイテレータのコピーを返す。

引数

  • x -- 進行の出発位置を示すイテレータ
  • n -- 進める距離
  • bound -- 進行の目的地となる位置を示すイテレータ(あるいは番兵)

効果

戻り値

  • (1) : xを1進めたイテレータのコピーを返す
  • (2) : xn進めたイテレータのコピーを返す
  • (3) : xboundまで進めたイテレータのコピーを返す
  • (4) : xbound以内でn進めたイテレータのコピーを返す

備考

この関数テンプレートは通常の名前探索で発見されている場合にADLを無効化する。詳しくは「ADLを無効にする関数定義」を参照のこと。

#include <iostream>
#include <iterator>
#include <vector>

int main() {
  std::vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

  auto it = std::begin(vec);

  // (1)、1つ進める
  auto pos2 = std::ranges::next(it);
  std::cout << *pos2 << std::endl;

  // (2)、3進める
  auto pos4 = std::ranges::next(it, 3);
  std::cout << *pos4 << std::endl;

  // (2)、-3進める
  auto pos1 = std::ranges::next(pos4, -3);
  std::cout << *pos1 << std::endl;


  auto bound = std::ranges::next(it, 5); // 6の位置

  // (3)、boundまで進める
  auto pos6 = std::ranges::next(it, bound);
  std::cout << *pos6 << std::endl;

  // (4)、boundまでの間で、8進める
  auto pos6_2 = std::ranges::next(it, 8, bound);
  std::cout << *pos6_2 << std::endl;

  // (4) boundまでの間で、4進める
  auto pos5 = std::ranges::next(it, 4, bound);
  std::cout << *pos5 << std::endl;
}

出力

2
4
1
6
6
5

バージョン

言語

  • C++20

処理系

関連項目

名前 説明
next() n回前方に進めたイテレータを返す
prev() n回後方に進めたイテレータを返す
advance() n回イテレータを進める
rangse::prev() n回後方に進めたイテレータを返す
ranges::advance() n回あるいはboundまでイテレータを進める

参照