C++ std::none_of() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++11

Definition

In C++, std::none_of() returns true when not a single element in the range matches the condition.

It reads naturally for rules like "no empty names" or "no negative scores". An empty range returns true.

Syntax

Add #include <algorithm>, then call std::none_of(). Start with these two lines.

C++
std::none_of(v.begin(), v.end(), pred);
std::none_of(s.begin(), s.end(), pred);
CallWhat it does
std::none_of(v.begin(), v.end(), pred);Checks every element in the range.
std::none_of(s.begin(), s.end(), pred);Also works on the characters of a string.
Library declarations
C++
template<class InputIt, class UnaryPred>
bool none_of(InputIt first, InputIt last, UnaryPred p);

Parameters

ParameterDescription
first, lastRequired. The range to check.
pRequired. A predicate that returns true for the elements you do not want.

Return Value

Return ValueDescription
trueNo element matched, or the range is empty.
falseAt least one element matched.

C++ Examples

Example 1 — No even numbers

Confirm that a list holds only odd values.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 3, 5, 7};
    bool none = std::none_of(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
    std::cout << (none ? "No even numbers" : "Found an even number") << '\n';
    return 0;
}

How It Works

  1. The predicate describes what we do not want: even numbers.
  2. Every value is odd, so the predicate is false each time.
  3. No match means none_of() returns true.

Example 2 — One bad value

A single match is enough to make the answer false.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {4, 9, 12};
    bool allSmall = std::none_of(v.begin(), v.end(), [](int n) { return n > 10; });
    if (!allSmall) {
        std::cout << "At least one value is above 10\n";
    }
    return 0;
}

How It Works

  1. n > 10 marks the forbidden values.
  2. 4 and 9 are fine, but 12 matches, so the search ends there.
  3. none_of() returns false, and !allSmall makes the message print.

Example 3 — Check text

Test the characters inside a string.

C++
#include <algorithm>
#include <iostream>
#include <string>

int main() {
    std::string pw = "code2026";
    bool noSpace = std::none_of(pw.begin(), pw.end(), [](char c) { return c == ' '; });
    std::cout << (noSpace ? "Password has no spaces" : "Remove the spaces") << '\n';
    return 0;
}

How It Works

  1. A std::string is a range of char, so begin() and end() work on it.
  2. The lambda checks each character against a space.
  3. None of the 8 characters is a space, so the password is accepted.

Frequently Asked Questions

In C++, std::none_of() checks that no element in a range matches a condition. Include <algorithm> to use it.
Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
It returns true when no element matches the predicate (or the range is empty), and false when at least one matches.
Yes. none_of(first, last, p) returns the same value as !any_of(first, last, p).
It returns true, because there is no element that could match.

Next: nth_element()

Puts the element that would be at position n in a sorted range into place.

nth_element() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful