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

履歴 編集

class template
<iterator>

std::move_sentinel(C++20)

namespace std {
  template<semiregular S>
  class move_sentinel {

  private:
    S last; // 説明専用メンバ変数
  };
}

概要

move_sentinelは、move_iteratorと共に任意のイテレータと番兵のペアをラップして、要素をムーブする範囲を表すための番兵アダプタである。

イテレータ型と番兵型が異なり、番兵型がイテレータとしての要件を満たさない場合終端を指すmove_iteratorを構成する事が出来ない。その場合にこのクラスを利用する事で、move_iteratorの終端を構成する事が出来るようになる。

任意の入力イテレータ型Iとその番兵型Ssentinel_for<S, I>のモデルである時、move_iterator<I>move_sentinel<S>もまたsentinel_for<move_sentinel<S>, move_iterator<I>>のモデルとなる。

メンバ関数

名前 説明 対応バージョン
(constructor) コンストラクタ C++20
operator= 代入演算子 C++20
base 元の番兵を取得する C++20

move_iteratorとの間の操作

名前 説明 対応バージョン
operator== 等値比較 C++20
operator!= 非等値比較(==により使用可能) C++20
operator- move_iteratorとの距離を求める C++20

利用例

例えば次のようなアルゴリズムを構成する際に利用する事ができる。

#include <iostream>
#include <algorithm>
#include <iterator>

// 範囲[first, last)から条件を満たす要素だけをムーブしてoutへ出力する
template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O, std::indirect_unary_predicate<I> Pred>
  requires std::indirectly_movable<I, O>
void move_if(I first, S last, O out, Pred pred) {
  // 番兵型SがIと異なり、イテレータ要件を満たさなかったとしても、move_iterator<I>の終端として扱う事ができる
  std::ranges::copy_if(std::move_iterator<I>{first}, std::move_sentinel<S>{last}, out, pred);
}

バージョン

言語

  • C++20

処理系

関連項目

参照