C++ <algorithm> std::none_of
The std::none_of
function template in C++ checks if none of the elements in a given range satisfy a specified condition. It is part of the C++ Standard Library’s <algorithm> header and is useful for determining if a particular predicate does not hold for any elements in a sequence.
Syntax of std::none_of
template <class InputIterator, class UnaryPredicate>
bool none_of(InputIterator first, InputIterator last, UnaryPredicate pred);
Parameters of std::none_of
Parameter | Description |
---|---|
first | An input iterator to the initial position in a sequence. |
last | An input iterator to the final position in a sequence (one past the last element). |
pred | A unary function that accepts an element in the range as an argument and returns a value convertible to bool . The value returned indicates whether the element satisfies the condition checked by this function. The function shall not modify its argument. |
Return Value of std::none_of
Returns true
if the predicate pred
returns false
for all elements in the range [first, last)
; otherwise, returns false
. If the range is empty, the function returns true
.
Exceptions for std::none_of
Throws an exception if either pred
or an operation on an iterator throws an exception. Note that invalid parameters cause undefined behavior.
Examples for none_of
Example 1: Using std::none_of to Check for Negative Numbers
In this example, we use std::none_of
to determine if none of the elements in a vector are negative numbers.
Program
#include <iostream>
#include <vector>
#include <algorithm> // For std::none_of
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
bool no_negatives = std::none_of(numbers.begin(), numbers.end(), [](int i) {
return i < 0;
});
if (no_negatives) {
std::cout << "There are no negative elements in the range." << std::endl;
} else {
std::cout << "There are some negative elements in the range." << std::endl;
}
return 0;
}
Output
There are no negative elements in the range.
Explanation
- We include the necessary headers:
<iostream>
for input/output operations,<vector>
for thestd::vector
container, and<algorithm>
for thestd::none_of
function. - We define a vector
numbers
containing the integers 1, 2, 3, 4, and 5. - We use
std::none_of
with a lambda function to check if none of the elements in the vector are less than 0. - If
no_negatives
istrue
, we print “There are no negative elements in the range.”; otherwise, we print “There are some negative elements in the range.”
Example 2: Using std::none_of with an Empty Range
This example demonstrates that std::none_of
returns true
when applied to an empty range.
Program
#include <iostream>
#include <vector>
#include <algorithm> // For std::none_of
int main() {
std::vector<int> empty_vector;
bool result = std::none_of(empty_vector.begin(), empty_vector.end(), [](int i) {
return i < 0;
});
std::cout << "Result for empty vector: " << std::boolalpha << result << std::endl;
return 0;
}
Output
Result for empty vector: true
Explanation
- We include the necessary headers:
<iostream>
for input/output operations,<vector>
for thestd::vector
container, and<algorithm>
for thestd::none_of
function. - We define an empty vector
empty_vector
. - We use
std::none_of
with a lambda function to check if none of the elements in the empty vector are less than 0. Since the range is empty,std::none_of
returnstrue
. - We print the result, which is
true
, indicating that no elements satisfy the condition in an empty range.
Example 3: Using std::none_of with a Custom Predicate
In this example, we define a custom predicate function to check if none of the elements in a list are odd numbers.
Program
#include <iostream>
#include <list>
#include <algorithm>
bool is_odd(int n) {
return n % 2 != 0;
}
int main() {
std::list numbers = {2, 4, 6, 8, 10};
bool no_odds = std::none_of(numbers.begin(), numbers.end(), is_odd);
if (no_odds) {
std::cout << "There are no odd numbers in the list." << std::endl;
} else {
std::cout << "There are some odd numbers in the list." << std::endl;
}
return 0;
}
Output
There are no odd numbers in the list.
Explanation
- We include the necessary headers:
<iostream>
for input/output operations,<list>
for thestd::list
container, and<algorithm>
for thestd::none_of
function. - A custom predicate function
is_odd
is defined, which returnstrue
if a number is odd andfalse
otherwise. - A list
numbers
is initialized with even numbers: 2, 4, 6, 8, and 10. - The
std::none_of
function is used with the custom predicate to check if none of the numbers in the list are odd. - Since all numbers are even,
no_odds
istrue
, and the program outputs “There are no odd numbers in the list.”
Examples for Exceptions thrown by algorithm none_of function
Example 1: Predicate Throws an Exception
This example demonstrates a case where the predicate function throws an exception during execution.
Program
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdexcept>
bool faulty_predicate(int n) {
if (n < 0) {
throw std::runtime_error("Negative number encountered.");
}
return n % 2 != 0;
}
int main() {
std::vector numbers = {2, 4, -6, 8, 10};
try {
bool result = std::none_of(numbers.begin(), numbers.end(), faulty_predicate);
std::cout << "No elements satisfy the condition: " << std::boolalpha << result << std::endl;
} catch (const std::runtime_error& e) {
std::cout << "Exception caught: " << e.what() << std::endl;
}
return 0;
}
Output
Exception caught: Negative number encountered.
Explanation
- The program defines a predicate function
faulty_predicate
that throws astd::runtime_error
if a negative number is encountered. - A vector
numbers
is initialized, including a negative number. - The
std::none_of
function applies the predicate to each element in the vector. When it encounters the negative number, the predicate throws an exception. - The exception is caught in the try-catch block, and an error message is printed.
Example 2: Iterator Throws an Exception
This example demonstrates a case where a custom iterator throws an exception during iteration.
Program
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdexcept>
class FaultyIterator {
public:
using iterator_category = std::input_iterator_tag;
using value_type = int;
using difference_type = std::ptrdiff_t;
using pointer = const int*;
using reference = const int&;
FaultyIterator(const int* ptr, bool fail_after = false)
: ptr(ptr), fail_after(fail_after), count(0) {}
reference operator*() const {
if (fail_after && count >= 2) {
throw std::runtime_error("Iterator error.");
}
return *ptr;
}
FaultyIterator& operator++() {
++ptr;
++count;
return *this;
}
bool operator!=(const FaultyIterator& other) const {
return ptr != other.ptr;
}
bool operator==(const FaultyIterator& other) const {
return ptr == other.ptr;
}
private:
const int* ptr;
bool fail_after;
int count;
};
int main() {
int arr[] = {1, 2, 3};
try {
bool result = std::none_of(
FaultyIterator(arr, true),
FaultyIterator(arr + 3),
[](int n) { return n < 0; });
std::cout << "No elements satisfy the condition: " << std::boolalpha << result << std::endl;
} catch (const std::runtime_error& e) {
std::cout << "Exception caught: " << e.what() << std::endl;
}
return 0;
}
Output
Exception caught: Iterator error.
Explanation
- The program defines a custom iterator
FaultyIterator
that throws astd::runtime_error
after two iterations. - An array
arr
is initialized with three elements. - The
std::none_of
function applies a lambda predicate to the elements. The iterator throws an exception during iteration. - The exception is caught in the
try-catch
block, and an error message is printed.