This tutorial will discuss about unique ways to check if all elements in array are in another array in C++.
Table Of Contents
Technique 1: using std::all_of()
Suppose we have two arrays, and now we want to check if all elements of one array are present in another array or not.
For this we are going to use the std::all_of()
function from STL algorithms. In the std::all_of()
function we will pass 3 arguments,
- Iterator pointing to the start of first array
- Iterator pointing to the end of first array
- A Lambda function which accepts a value, and returns true, if this value exists in this second array.
- We are going to use the STL algorithm
std::find()
inside the Lambda function to check if the given value exists in the second array or not.
- We are going to use the STL algorithm
The std::all_of()
function will apply the given Lambda function on all the elements of the first array. If the Lambda function returns true for all the elements then the std::all_of()
function will return true. It means all elements of first array are present in the second array.
Let’s see the complete example,
#include <iostream> #include <algorithm> int main() { int arr1[] = { 31, 32, 33, 34}; int arr2[] = { 34, 33, 89, 98, 31, 20, 32}; // Check if all elements of arr1 exists in the arr2 bool result = std::all_of( std::begin(arr1), std::end(arr1), [&arr2](int elem){ return std::find( std::begin(arr2), std::end(arr2), elem) != std::end(arr2); } ); if (result) { std::cout << "Yes, all elements of first array are present in the second array." << std::endl; } else { std::cout << "No, all elements of first array are not present in the second array." << std::endl; } return 0; }
Output :
Frequently Asked:
Yes, all elements of first array are present in the second array.
Technique 2: using std::includes()
The std::includes()
function accepts two ranges as arguments. Basically the first two arguments points to the start & end of second array and the next two arguments points to the start & end of of first range. It returns true if all the elements of the first strange exist in the second range.
So to check if all elements are first array are present in another array, we will pass 4 arguments. i.e.
- Iterator pointing to the start of second array.
- Iterator pointing to the end of second array.
- Iterator pointing to the start of first array.
- Iterator pointing to the end of first array.
It will return true if all the elements of first array are present in the second array.
Let’s see the complete example,
Pointers in C/C++ [Full Course]
#include <iostream> #include <algorithm> int main() { int arr1[] = { 31, 32, 33, 34}; int arr2[] = { 34, 33, 89, 98, 31, 20, 32}; // Sort first array std::sort(std::begin(arr1), std::end(arr1)); // Sort second array std::sort(std::begin(arr2), std::end(arr2)); // Check if all elements of arr1 exists in the arr2 bool result = std::includes( std::begin(arr2), std::end(arr2), std::begin(arr1), std::end(arr1)); if (result) { std::cout << "Yes, all elements of first array are present in the second array." << std::endl; } else { std::cout << "No, all elements of first array are not present in the second array." << std::endl; } return 0; }
Output :
Yes, all elements of first array are present in the second array.
Summary
Today we learned about several ways to check if all elements in array are in another array in C++. Thanks.