C++ 20

Info

std::none_of checks to see if a given unary predicate returns false for all of the values in a range

Complexity: Linear, O(n)

Category: Non-Modifying Sequence Operations

Breakdown

std::none_of:

template<class InputIterator, class Predicate>
constexpr bool none_of(InputIterator first
,InputIterator last
,Predicate pred);

Parameters

Returns

std::ranges::none_of:

template<ranges::input_range Range, class Predicate>
constexpr bool none_of(Range &&r, Predicate pred);

Parameters

Returns

Use Cases

Examples

#include <algorithm>
#include <list>
#include <string>
#include <iostream>
int main()
{
std::list<int> l1({12, 9, 2, -3, 65});
std::list<std::string> l2({"dog", "cat", "bear", "xylophone"});
//Iterator none_of, returns true if no value is less than 0
bool positive = std::none_of(l1.begin()
,l1.end()
,[](int x)->bool
{return x<0;});
//Ranges none_of, returns true if no value is "fish"
bool missing = std::ranges::none_of(l2,[](std::string s)->bool
{return s=="fish";});
//Iterator none_of, returns true if no value is longer than 5
bool len = std::none_of(l2.begin()
,l2.end()
,[](std::string s)->bool
{return s.size()>=5;});
std::cout << "positive: " << positive << '\n';
std::cout << "missing: " << missing << '\n';
std::cout << "len: " << len << '\n';
}
Output
positive: 0
missing: 1
len: 0

Keywords

Sources

Im just here to learn some things