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

履歴 編集

function template
<algorithm>

std::ranges::equal(C++20)

namespace std::ranges {
  template <input_iterator I1,
            sentinel_for<I1> S1,
            input_iterator I2,
            sentinel_for<I2> S2,
            class Pred = ranges::equal_to,
            class Proj1 = identity,
            class Proj2 = identity>
    requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
  constexpr bool
    equal(I1 first1,
          S1 last1,
          I2 first2,
          S2 last2,
          Pred pred = {},
          Proj1 proj1 = {},
          Proj2 proj2 = {}); // (1) C++20

  template <input_range R1,
            input_range R2,
            class Pred = ranges::equal_to,
            class Proj1 = identity,
            class Proj2 = identity>
    requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
  constexpr bool
    equal(R1&& r1,
          R2&& r2,
          Pred pred = {},
          Proj1 proj1 = {},
          Proj2 proj2 = {}); // (2) C++20
}

概要

2つの範囲を等値比較する。

2つの範囲の要素数および各要素が等値であった場合、trueを返す。

戻り値

2つの範囲の長さを定数時間で求められ、もし last1 - first1 != last2 - first2 であれば、false を返す。

そうでない場合、[first1,last1) 内のイテレータ i について、invoke(pred,invoke(proj1, *i),invoke(proj2, *(first2 + (i - first1)))) != false が全てのイテレータ i について満たされているのであれば true を返す。
そうでない場合は false を返す。

計算量

最大で min(last1 - first1, last2 - first2) 回の述語が適用される。ただし、2つの範囲の長さを定数時間で求められ、かつ、長さが異なる場合、1 度も述語は適用されない。

#include <algorithm>
#include <iostream>
#include <vector>
#include <array>
#include <iterator>

int main() {
  std::vector<int>   v  = { 1,2,3,4,3,2 };
  std::array<int, 6> v2 = { 1,2,3,4,2,1 };

  // コンテナの中身が同じかどうか調べる
  bool result = std::ranges::equal(v, v2);
  std::cout << std::boolalpha << result << std::endl;

  // x±1 の誤差を許すようにする
  bool result2 = std::ranges::equal(v, v2, [](int x, int y) { return x - 1 <= y && y <= x + 1; });
  std::cout << std::boolalpha << result2 << std::endl;
}

出力

false
true

実装例

struct equal_impl {
  template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2, class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
    requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
  constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const {
    if constexpr (sized_sentinel_for<S1, I1> && sized_sentinel_for<S2, I2>)
      if (distance(first1, last1) != distance(first2, last2))
        return false;
    for ( ; first1 != last1 && first2 != last2; ++first1, ++first2)
      if (!bool(invoke(pred, invoke(proj1, *first1), invoke(proj2, *first2))))
        return false;
    return true;
  }

  template<input_range R1, input_range R2, class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
    requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
  constexpr bool operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const {
    return (*this)(begin(r1), end(r1), begin(r2), end(r2), ref(pred), ref(proj1), ref(proj2));
  }
};

inline constexpr equal_impl equal;

バージョン

言語

  • C++20

処理系

参照