4

Check if All Numbers in Array are Less than a Number in C++

 1 year ago
source link: https://thispointer.com/check-if-all-numbers-in-array-are-less-than-a-number-in-c/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Check if All Numbers in Array are Less than a Number in C++

This tutorial will discuss about a unique way to check if all numbers in array are less than a number in C++.

To check if all the elements of an array are less than a given number, we need to iterate over all the elements of array and check each element one by one.

For that we can use a STL Algorithm std::all_of(), which accepts the start & end iterators of an array as first two arguments. As this 3rd argument it will accept a Lambda function

This lambda function accept one argument and returns true, if the given argument is less than specific number, otherwise returns false.

The std::all_of() function will apply the given Lambda function on all the elements of array, and returns true only if the Lambda function returns true for all the elements of the array. The syntax is as follows,

Advertisements

Copy to clipboard
int arr[] = {8, 9, 6, 1, 2, 5, 10, 14};
int number = 20;
// Check if all numbers are less than a specific number
bool result = std::all_of(
std::begin(arr),
std::end(arr),
[&](const int& elem) {
return elem < number;
int arr[] = {8, 9, 6, 1, 2, 5, 10, 14};
int number = 20;

// Check if all numbers are less than a specific number
bool result = std::all_of(
                std::begin(arr),
                std::end(arr),
                [&](const int& elem) {
                    return elem < number;
                });

Here std::all_of() function function will return true, if all the numbers in the array are less than a specific number i.e. number 20.

Let’s see the complete example,

Copy to clipboard
#include <iostream>
#include <algorithm>
int main()
int arr[] = {8, 9, 6, 1, 2, 5, 10, 14};
int number = 20;
// Check if all numbers are less than a specific number
bool result = std::all_of(
std::begin(arr),
std::end(arr),
[&](const int& elem) {
return elem < number;
if(result)
std::cout << "Yes, all elements in the array are less than a specifc number \n";
std::cout << "No, all elements in the array are not less than a speciifc number" << std::endl;
return 0;
#include <iostream>
#include <algorithm>

int main()
{
    int arr[] = {8, 9, 6, 1, 2, 5, 10, 14};
    int number = 20;

    // Check if all numbers are less than a specific number
    bool result = std::all_of(
                    std::begin(arr),
                    std::end(arr),
                    [&](const int& elem) {
                        return elem < number;
                    });

    if(result)
    {
        std::cout << "Yes, all elements in the array are less than a specifc number \n";
    }
    else
    {
        std::cout << "No, all elements in the array are not less than a speciifc number" << std::endl;
    }
    return 0;
}

Output :

Copy to clipboard
Yes, all elements in the array are less than a specifc number
Yes, all elements in the array are less than a specifc number

Summary

Today we learned about a way to check if all numbers in array are less than a number in C++. Thanks.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK