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

履歴 編集

function
<iterator>

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

constexpr counted_iterator& operator--()
  requires bidirectional_iterator<I>;       // (1)

constexpr counted_iterator operator--(int)
  requires bidirectional_iterator<I>;       // (2)

概要

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

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

効果

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

  • (1) : 以下と等価

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

  • (2) : 以下と等価

    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};

  ++ci;
  ++ci;

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

  --ci;

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

  ci--;

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

出力

3
2
1

バージョン

言語

  • C++20

処理系

参照