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

履歴 編集

function
<iterator>

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

constexpr auto operator->() const noexcept
  requires contiguous_iterator<I>;

概要

イテレータを通して参照先の要素のメンバにアクセスする

効果

Iの値をcurrentメンバ変数に保持するとして、以下と等価

return to_address(current);

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

struct S {
  int n;

  S(int a) : n{a} {}

  void print() const {
    std::cout << this->n << '\n';
  }
};

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

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

  ci->print();

  ++ci;

  ci->print();
}

出力

1
2

バージョン

言語

  • C++20

処理系

参照