Skip to content

Latest commit

 

History

History
101 lines (77 loc) · 1.89 KB

op_increment.md

File metadata and controls

101 lines (77 loc) · 1.89 KB

operator++

  • iterator[meta header]
  • std[meta namespace]
  • counted_iterator[meta class]
  • function[meta id-type]
  • cpp20[meta cpp]
constexpr counted_iterator& operator++();   // (1)

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

constexpr counted_iterator operator++(int)
  requires forward_iterator<I>;             // (3)
  • forward_iterator[link /reference/iterator/forward_iterator.md]

概要

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

  • (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';
}
  • ++ci[color ff0000]
  • ci++[color ff0000]
  • ranges::begin[link /reference/ranges/begin.md]

出力

1
2
3

バージョン

言語

  • C++20

処理系

参照