Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of none_of function in C++

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 193
    Comment on it

    none_of function is used to test condition on the elements in a range[first,last), it will return true if the test condition is false for all of the element in the range or if the range is empty, else it will return false. To use this function we have to include "algorithm" header file into our program file. This function can be think of as an opposite of all_of function.


    Function Prototype

    bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred);
    


    About the parameters

    first
    It is the initial position of the range which is included in the range.

    last
    It is the last position in the range, which is not included in the range.


    pred
    It is a unary function which return value which is convertible to bool. This function is used to test condition on the elements in the range.



    Sample program using none_of

    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
            int arr_one[] = {1,3,5,7,9};
            int arr_two[] = {1,3,6,7,9};
    
            //applying none_of on the arr_one
            cout << "\nElements inside the range : ";
            for (int x:arr_one) {
                    cout << x << " ";
            }
            cout << endl;
            if ( none_of(arr_one,arr_one+5,[](int i){return !(i%2);})) {
                    cout << "All the elements in the range are odd.\n";
            } else {
                    cout << "All the elements in the range are not odd.\n";
            }
    
    
            //applying on the arr_two
            cout << "\nElements inside the range : ";
            for (int x:arr_two) {
                    cout << x << " ";
            }
            cout << endl;
            if ( none_of(arr_two,arr_two+5,[](int i){return !(i%2);})) {
                    cout << "All the elements in the range are odd.\n";
            } else {
                    cout << "All the elements in the range are not odd.\n";
            }
    
            return 0;
    }
    

    Output of the program

    Elements inside the range : 1 3 5 7 9 
    All the elements in the range are odd.
    
    Elements inside the range : 1 3 6 7 9 
    All the elements in the range are not odd.
    

    The complexity of all_of function is upto linear.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: