• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function template
    <algorithm>

    std::none_of

    namespace std {
      template <class InputIterator, class Predicate>
      bool none_of(InputIterator first,
                   InputIterator last,
                   Predicate pred);               // (1) C++11
    
      template <class InputIterator, class Predicate>
      constexpr bool none_of(InputIterator first,
                             InputIterator last,
                             Predicate pred);     // (1) C++20
    
      template <class ExecutionPolicy, class ForwardIterator, class Predicate>
      bool none_of(ExecutionPolicy&& exec,
                   ForwardIterator first,
                   ForwardIterator last,
                   Predicate pred);               // (2) C++17
    }
    

    概要

    イテレータ範囲[first, last)の全ての要素が条件を満たさないかを判定する。

    戻り値

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

    計算量

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

    備考

    この関数は

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

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

    #include <algorithm>
    #include <iostream>
    #include <vector>
    
    int main() {
      std::vector<int> v = { 3, 1, 4 };
    
      std::cout << std::boolalpha;
    
      // 全ての要素が 3 以上であるか
      bool result1 = std::none_of(v.begin(), v.end(), [](int x) { return x < 3; });
      std::cout << result1 << std::endl;
    
      // 全ての要素が 0 以外であるか
      bool result2 = std::none_of(v.begin(), v.end(), [](int x) { return x == 0; });
      std::cout << result2 << std::endl;
    }
    

    出力

    false
    true
    

    実装例

    template <class InputIterator, class Predicate>
    bool none_of(InputIterator first, InputIterator last, Predicate pred) {
      for ( ; first != last; ++first)
        if (pred(*first)) return false;
      return true;
    }
    

    バージョン

    言語

    • C++11

    処理系

    関連項目

    参照