std::none_of in C++ Last Updated : 20 Feb, 2023 Comments Improve Suggest changes Like Article Like Report Returns true if pred returns false for all the elements in the range [first, last] or if the range is empty, and false otherwise.Syntax : Template : bool none_of(InputIterator first, InputIterator last, UnaryPredicate pred); first, last Input iterators to the initial and final positions in a sequence. The range used is [first, last], which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. pred Unary function that accepts an element in the range as argument and returns a value convertible to bool. The value returned indicates whether the element fulfills the condition checked by this function. The function shall not modify its argument. This can either be a function pointer or a function object. Return type : true if pred returns false for all the elements in the range [first, last] or if the range is empty, and false otherwise. CPP // CPP program to illustrate std :: none_of #include <iostream> // cout #include <algorithm> // none_of using namespace std; // function to check whether the // element is negative or not bool comp(int a) { return a < 0; } // Driver code int main() { int arr[] = { 2, 4, 6, 8, 12, 0 }; int n = sizeof(arr)/sizeof(arr[0]); cout << "Array contains :"; for (int i = 0; i < n; i++) cout << ' ' << arr[i]; cout << "\n"; if (none_of(arr, arr+n, comp)) cout << "No negative elements in the range.\n"; else cout << "There is at least one negative" " element in the array range.\n"; return 0; } OutputArray contains : 2 4 6 8 12 0 No negative elements in the range. Time Complexity: O(n) Auxiliary Space: O(1) Practical Application : std :: none_of function returns true if certain condition returns false for all the elements in the range [first, last] or if the range is empty, and false otherwise.1. Check if array contains all even number or odd or both. CPP // CPP program to illustrate std :: none_of #include <iostream> // cout #include <algorithm> // none_of using namespace std; // functions to check whether the // element is even or odd bool isEven(int a) { return (a % 2); } bool isOdd(int a) { return (a % 2 == 0); } // Driver code int main() { int arr[] = { 2, 4, 6, 8, 12, 0 }; int n = sizeof(arr)/sizeof(arr[0]); cout << "Array contains :"; for (int i = 0; i < n; i++) cout << ' ' << arr[i]; cout << "\n"; bool even = none_of(arr, arr + n, isEven); bool odd = none_of(arr, arr + n, isOdd); if ((!even) && (!odd)) cout << "Contains both even and" " odd number\n"; else if ((!even) && odd) cout << "Contains odd number only\n"; else if (even && (!odd)) cout << "Contains even number only\n"; else cout << "Array is empty\n"; return 0; } OutputArray contains : 2 4 6 8 12 0 Contains even number only Time Complexity: O(n) Auxiliary Space: O(1) 2. To check whether array contains all prime number or not. CPP // CPP program to illustrate std :: none_of #include <algorithm> // none_of #include <iostream> // cout using namespace std; // Function reference : // https://www.geeksforgeeks.org/primality-test-set- // 1-introduction-and-school-method/ bool isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Driver code int main() { int arr[] = { 4, 6, 8, 12, 0 }; int n = sizeof(arr) / sizeof(arr[0]); cout << "Array contains :"; for (int i = 0; i < n; i++) cout << ' ' << arr[i]; cout << "\n"; if (none_of(arr, arr + n, isPrime)) cout << "All numbers are composite.\n"; else cout << "There are primes in array \n"; return 0; } OutputArray contains : 4 6 8 12 0 All numbers are composite. Time Complexity: O(n) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article std::none_of in C++ S Sachin Bisht Improve Article Tags : C++ DSA cpp-algorithm-library Practice Tags : CPP Similar Reads std::all_of() in C++ The C++ function is defined in <algorithm> library in STL. This function operates on whole range of array elements and can save time to run a loop to check each elements one by one. It checks for a given property on every element and returns true when each element in range satisfies specified 2 min read bitset none() in C++ STL bitset::none() is a built-in STL in C++ which returns True if none of its bits are set. It returns False if a minimum of one bit is set. Syntax: bool none() Parameter: The function accepts no parameter. Return Value: The function returns a boolean. The boolean value is True if none of its bits are s 2 min read set::size() in C++ STL In C++, set::size() function is a built-in used to find the number of elements in the given set container. It is the member function of std::set class defined inside <set> header file. In this article, we will learn about the std::set::size() method in C++.Example:C++// C++ Program to illustra 2 min read any_of() Function in C++ STL any_of() is the C++ function defined in <algorithm> library in STL. This function determines whether even one element in a given range satisfies a specified criterion. If at least one element meets the property, then it returns true; otherwise, it returns false. Also if the range is empty then 2 min read std::string::find_last_not_of in C++ It searches the string for the first character, from the end of the string, that does not match any of the characters specified in its arguments. Return value : Index of first unmatched character when successful or string::npos if no such character found. Syntax 1: Search for the last character that 6 min read std::string::find_first_not_of in C++ It searches the string for the first character that does not match any of the characters specified in its arguments. Here we will describe all syntaxes it holds. Return value : Index of first unmatched character when successful or string::npos if no such character found. Syntax 1: Search for the fir 6 min read NULL Pointer in C++ A NULL Pointer in C++ indicates the absence of a valid memory address in C++. It tells that the pointer is not pointing to any valid memory location In other words, it has the value "NULL" (or 'nullptr' since C++11). This is generally done at the time of variable declaration to check whether the poi 4 min read std::find_if , std::find_if_not in C++ std :: find_if and std :: find_if_not are algorithm functions in C++ Standard Library in <algorithm> header file. These functions provide an efficient way to search for an element in a container using a predicate function. std :: find_if This function returns an iterator to the first element i 3 min read set::empty() in C++ STL Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::empty() empty() 2 min read set::clear in C++ STL Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::clear() clear() 2 min read Like