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

履歴 編集

function template
<algorithm>

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

namespace std::ranges {
  template <input_iterator I,
            sentinel_for<I> S,
            class Proj = identity,
            indirect_unary_predicate<projected<I, Proj>> Pred>
  constexpr bool
    none_of(I first,
            S last,
            Pred pred,
            Proj proj = {}); // (1) C++20

  template <input_range R,
            class Proj = identity,
            indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
  constexpr bool
    none_of(R&& r,
            Pred pred,
            Proj proj = {}); // (2) C++20

  template <execution-policy Ep,
            random_access_iterator I,
            sized_sentinel_for<I> S,
            class Proj = identity,
            indirect_unary_predicate<projected<I, Proj>> Pred>
  bool none_of(Ep&& exec,
               I first,
               S last,
               Pred pred,
               Proj proj = {}); // (3) C++26

  template <execution-policy Ep,
            sized-random-access-range R,
            class Proj = identity,
            indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
  bool none_of(Ep&& exec,
               R&& r,
               Pred pred,
               Proj proj = {}); // (4) C++26
}

概要

範囲の全ての要素が条件を満たさないかを判定する。

  • (1): イテレータ範囲を指定する
  • (2): Rangeを直接指定する
  • (3): (1)の並列アルゴリズム版。実行ポリシーを指定する
  • (4): (2)の並列アルゴリズム版。実行ポリシーを指定する

テンプレートパラメータ制約

戻り値

[first,last) あるいは r が空であったり、範囲内の全てのイテレータ i について invoke(pred, invoke(proj, *i))false である場合は true を返し、そうでない場合は false を返す。

計算量

最大で last - firstpred を実行する。

備考

この関数は

all_of(first, last, not_fn(pred));

とほぼ同じであるが、全ての要素が条件を満たしていないということを明示したい場合は none_of() を使う方が意図が伝わりやすい。

基本的な使い方

#include <algorithm>
#include <iostream>
#include <array>

int main() {
  constexpr std::array v = { 3, 1, 4 };

  std::cout << std::boolalpha;

  // 全ての要素が 3 以上であるか
  constexpr bool result1 = std::ranges::none_of(v, [](int x) { return x < 3; });
  std::cout << result1 << std::endl;

  // 全ての要素が 0 以外であるか
  constexpr bool result2 = std::ranges::none_of(v, [](int x) { return x == 0; });
  std::cout << result2 << std::endl;
}

出力

false
true

並列アルゴリズムの例 (C++26)

#include <algorithm>
#include <execution>
#include <iostream>
#include <vector>

int main() {
  std::vector<int> v = {1, 3, 5, 7, 9};

  std::cout << std::boolalpha;

  // 並列に全ての要素が偶数でないかを判定
  bool result = std::ranges::none_of(std::execution::par, v,
                                     [](int x) { return x % 2 == 0; });
  std::cout << result << std::endl;
}

出力

true

実装例

struct none_of_impl {
  template<input_iterator I, sentinel_for<I> S, class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred>
  constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const {
    for ( ; first != last; ++first)
      if (invoke(pred, invoke(proj, *first))) return false;
    return true;
  }

  template<input_range R, class Proj = identity, indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
  constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const {
    return (*this)(begin(r), end(r), ref(pred), ref(proj));
  }
};

inline constexpr none_of_impl none_of;

バージョン

言語

  • C++20

処理系

関連項目

参照