Fluent::Programmer

    Home Blog About
  • Home
  • Blog
  • About
  • c++
  • beautiful-code-series
  • linux
  • algorithms
  • assembly-language
  • c-programming
  • network-programming

Beautiful code #4 - std::any_of, std::none_of, std::all_of - efficient C++20 featuređź‘‹

  • Fluent Programmer
  • March 4, 2023
  •   3 min read

Quick summary ↬  std::any_of, std::none_of and std::all_of is a C++20 feature that helps efficiently check for basic conditions in the data structures. This is very optimal because of early termination once some fact is found and the algorithm don’t have to iterate more to further prove anything before returning the result.

beautiful-code-5

Beautiful code #4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <vector>
#include <algorithm>
#include <iostream>

int main() {
    std::vector<int> data{2,2,2,4,6,8};
    
    auto is_even = [](int v) {return v % 2 == 0; };
    auto is_odd = [](int v) {return v % 2 != 0; };
    auto is_double_digit = [](int v) { return v > 9 && v < 100; };

    bool output_1 = std::all_of(data.begin(), data.end(), is_even);
    // output_1 is true in our example

    std::cout << std::boolalpha << output_1 << std::endl;
    bool output_2 = std::none_of(data.begin(), data.end(), is_odd);
    // output_2 is true in our example

    std::cout << std::boolalpha << output_2 << std::endl;

    bool output_3 = std::any_of(data.begin(), data.end(), is_double_digit);
    // output_3 is false in our example

    std::cout << std::boolalpha << output_3 << std::endl;

    int count = 0;
    auto second_count = [&count](int v) {
        ++count;
        return v < 6;
    };

    bool output_4 = std::all_of(data.begin(), data.end(), second_count);
    std::cout << std::boolalpha << output_4 << ", count is " << count << std::endl;

}

Beautiful code #4 Explanation

The above C++ code snippet make use of all the three functions std::any_of, std::none_of, std::none_of.

std::all_of

std::all_of as used in bool output_1 = std::all_of(data.begin(), data.end(), is_even); iterates through the vector data until it find an evidence to return a boolean false. In the above std::all_of code, the third argument is a lambda function that returns true whenever the value is even. If it finds and odd the std::all_of does an early termination and returns false.

If we pass an empty vector then the std::all_of returns false.

1
2
3
4
5
6
7
8
int count = 0;
    auto second_count = [&count](int v) {
        ++count;
        return v < 6;
    };

    bool output_4 = std::all_of(data.begin(), data.end(), second_count);
    std::cout << std::boolalpha << output_4 << ", count is " << count << std::endl;

The above code proves that std::all_of has early termination in place.

std::none_of

std::none_of as used in bool output_2 = std::none_of(data.begin(), data.end(), is_odd); iterates through the vector data to make sure none of the value is an odd. If any value is odd then the function std::none_of returns false else true.

If we pass an empty vector then the std::none_of returns true by default..

std::any_of

std::any_of as used in bool output_3 = std::any_of(data.begin(), data.end(), is_double_digit); iterates through the vector and check for any double digit value. If it find any that met the condition then it returns true else false.

If we pass an empty vector then the std::any_of returns false by default.

Conclusion

All these algorithms has worst case runtime of O(n) and I find it beautiful. Hope you enjoyed it !!

About The Author

Fluentprogrammer doesn't need coffee to program. They run on pure caffeine and lines of code.

Email Newsletter

Table of Contents

  • Beautiful code #4
  • Beautiful code #4 Explanation
    • std::all_of
    • std::none_of
    • std::any_of
  • Conclusion
  • C++
  • Systems & Network
  • C programming
  • Beautiful code
  • Design patterns
  • Linux
  • Open Source
  • Algorithms
  • Data Structures
  • System design
  • Distributed systems
  • Kernel
  • Assembly language
  • Hardware
  • Ultra Low Latency
  • Inspiration

Unhealthy love with dark corners of C++

Founded by an engineer to help engineers. 2021–2023.

Fluentprogrammer.com is a brand name managed by Abyan, Inc.

  • About us
  • Privacy policy