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

履歴 編集

function
<ranges>

std::ranges::zip_view::size(C++23)

constexpr auto size()
  requires (sized_range<Views> && ...);       // (1) C++23

constexpr auto size() const
  requires (sized_range<const Views> && ...); // (2) C++23

概要

要素数を取得する。

効果

以下と等価:

return apply(
  [](auto... sizes) {
    using CT = make-unsigned-like-t<common_type_t<decltype(sizes)...>>;
    return ranges::min({CT(sizes)...});
  },
  tuple-transform(ranges::size, views_));

ここで、tuple-transformは説明専用の関数テンプレートである。

備考

zip_viewのサイズは、zipする各Rangeのサイズの最小値となる。

#include <ranges>
#include <vector>
#include <array>
#include <iostream>

int main() {
  std::vector<int> v = {1, 2, 3};
  std::array<char, 5> a = {'a', 'b', 'c', 'd', 'e'};

  std::ranges::zip_view zv(v, a);

  // サイズは小さい方のサイズ(3)になる
  std::cout << "size: " << zv.size() << std::endl;

  // const版も動作する
  const auto& czv = zv;
  std::cout << "const size: " << czv.size() << std::endl;
}

出力

size: 3
const size: 3

バージョン

言語

  • C++23

処理系

参照