GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::ranges::next - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. iterator
  3. next

最終更新日時: 2026年09月19日 21時31分51秒 (JST)
rotarymars が更新

履歴 編集

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;
    return x;
    

  • (2) : 以下と等価

    ranges::advance(x, n);
    return x;
    

  • (3) : 以下と等価

    ranges::advance(x, bound);
    return x;
    

  • (4) : 以下と等価

    ranges::advance(x, n, bound);
    return x;
    

戻り値

  • (1) : xを1進めたイテレータのコピーを返す
  • (2) : xをn進めたイテレータのコピーを返す
  • (3) : xをboundまで進めたイテレータのコピーを返す
  • (4) : xをbound以内で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

処理系

  • Clang: 13.0.1 ✅
  • GCC: 10.1 ✅
  • Visual C++: 2019 Update 5 ✅

関連項目

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

参照

  • P0896R4 The One Ranges Proposal (was Merging the Ranges TS)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.