std::includes() in C++ STL - GeeksforGeeks

23 Dec, 2021
includes() is a C++ function that can be used to recognize if all the numbers in a container, also exist in other containers. It helps to check whether a set is a subset of another set or not considering the set is ordered. The elements are expected to be in sorted order. This function is defined in <algorithm> header file. 
There are two implementations possible and are explained below:
 Template:
template 
  bool includes (initer1 beg1, initer1 end1,
                        initer2 beg2, initer2 end2,)
Parameters:
beg1:  Input iterator to initial position of first sorted sequence.
end1:  Input iterator to the final position of the first sorted sequence.
beg2:  Input iterator to initial position of the second sorted sequence.
end2:  Input iterator to the final position of the second sorted sequence.
Return value: True, if every element of 2nd container lies in 1st container.
It has linear time complexity.
CPP
// C++ code to demonstrate the working of includes()#include <bits/stdc++.h>using namespace std; // Driver Codeint main(){    // initializing 1st container    vector<int> arr1 = { 1, 4, 6, 3, 2 };     // initializing 2nd container    vector<int> arr2 = { 1, 2, 4 };     // sorting initial containers    sort(arr1.begin(), arr1.end());    sort(arr2.begin(), arr2.end());     // using include() check if all elements    // of arr2 lie in arr1    if (includes(arr1.begin(), arr1.end(), arr2.begin(),                 arr2.end()))        cout << "All elements of 2nd container are in 1st "                "container";    else        cout << "All elements of 2nd container are not in "                "1st container";}
All elements of 2nd container are in 1st container
Template:
template 
  bool includes (initer1 beg1, initer1 end1,
                        initer2 beg2, initer2 end2, Compare comp)
Parameters:
beg1:  Input iterator to initial position of first sorted sequence.
end1:  Input iterator to the final position of the first sorted sequence.
beg2:  Input iterator to initial position of the second sorted sequence.
end2:  Input iterator to the final position of the second sorted sequence.
comp: The comparator function that returns a boolean true/false of each element compared. This function accepts two arguments. This can be a function pointer or function object and cannot change values.
Return value: True if every element of the 2nd container lies in the 1st container.
CPP
// C++ code to demonstrate the working of// includes()#include <bits/stdc++.h>using namespace std; // comparator functionbool comp(int i, int j) { return i < j; } // Driver Codeint main(){    // initializing 1st container    vector<int> arr1 = { 1, 4, 6, 3, 2 };     // initializing 2nd container    vector<int> arr2 = { 1, 2, 4 };     // sorting initial containers    sort(arr1.begin(), arr1.end());    sort(arr2.begin(), arr2.end());     // using include() check if all elements    // of arr2 lie in arr1    // using comparator function    if (includes(arr1.begin(), arr1.end(), arr2.begin(),                 arr2.end(), comp))        cout << "All elements of 2nd container are in 1st "                "container";    else        cout << "All elements of 2nd container are not in "                "1st container";    return 0;}
All elements of 2nd container are in 1st container
Application: This function can be potentially used to find if a set is a subset of another or can be used to determine the lottery winner, in a system in which a person who has all the lottery numbers in his card wins the lottery. The latter one is explained below through code.
CPP
// C++ code to demonstrate the application of// includes()#include <bits/stdc++.h>using namespace std; // Driver Codeint main(){    // lottery numbers    vector<int> lottery = { 1, 4, 6, 3, 2, 54, 32 };     // Numbers in user's card    vector<int> user = { 1, 2, 4, 6 };     // sorting initial containers    sort(lottery.begin(), lottery.end());    sort(user.begin(), user.end());     // using include() check if all elements    // of user are present as lottery numbers    if (includes(lottery.begin(), lottery.end(),                 user.begin(), user.end()))        cout << "User has won lottery ( all numbers are "                "lottey numbers )";    else        cout << "User has not won the lottery";}
User has won lottery ( all numbers are lottey numbers )
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
anshikajain26
cpp-algorithm-library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Iterators in C++ STL
Operator Overloading in C++
Friend class and function in C++
Polymorphism in C++
Sorting a vector in C++
Convert string to char array in C++
Inline Functions in C++
List in C++ Standard Template Library (STL)
std::string class in C++
new and delete operators in C++ for dynamic memory