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

履歴 編集

function
<iterator>

std::counted_iterator::operator++(C++20)

constexpr counted_iterator& operator++();   // (1)

decltype(auto) operator++(int);             // (2)

constexpr counted_iterator operator++(int)
  requires forward_iterator<I>;             // (3)

概要

イテレータをインクリメントする。

  • (1) : 前置インクリメント
  • (2)(3) : 後置インクリメント

事前条件

効果

現在のイテレータとカウントの値をそれぞれ、currentlengthメンバ変数に保持するとする。

  • (1) : 以下と等価

    ++current;
    --length;
    return *this;
    

  • (2) : 以下と等価

    --length;
    try { 
      return current++;
    } catch (...) {
      ++length;
      throw;
    }
    

  • (3) : 以下と等価

    counted_iterator tmp = *this;
    ++*this;  // (1)に委譲
    return tmp;
    

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

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

  std::counted_iterator ci{std::ranges::begin(vec), 5};

  std::cout << *ci << '\n';

  ++ci;

  std::cout << *ci << '\n';

  ci++;

  std::cout << *ci << '\n';
}

出力

1
2
3

バージョン

言語

  • C++20

処理系

参照