std::include() function in C++

Here we will learn more about std::include() function in C++. This function recognizes the matched numbers in both containers. The objective is achieved by “include”, defined in the header.

Let us look at an example that gives a brief explanation of the same:

Example-1 of std::include() function in C++

using namespace std;
int main() 
    vector<int> array1 = { 1, 2, 3,4, 5 }; 
    vector<int> array2 = { 1, 2, 3 }; 
    sort(array1.begin(), array1.end()); 
    sort(array2.begin(), array2.end()); 
    if(includes(array1.begin(), array1.end(), array2.begin(), array2.end())) 
    cout << "The elements are matched"; 
    cout << "The elements are not matched"; 

Output: The elements are matched

Example-2 of this function

using namespace std;
int main() 
    vector<int> a = { 1, 2, 3, 3, 4, 5 , 6 }; 
    vector<int> b = { 1, 2, 3, 4 }; 
    sort(a.begin(), a.end()); 
    sort(a.begin(), b.end()); 
    if(includes(a.begin(), a.end(), b.begin(), b.end())) 
    cout << "Matched"; 
    cout << "Not matched"; 

The examples match all the elements of the passes array to provide the result. Some real-life applications can be seen in the lottery decisions or in card games. In the code, it is necessary to sort the elements first. It also throws an exception on an operation on an iterator and it causes undefined behavior for invalid parameters. It gives linear time complexity.

Example-3 Complete code for std::include() function

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
    vector<int> vector1 = {1, 2, 3, 4, 5};
   vector<int> vector2 = {1, 2, 3};
   bool test;

   test = includes(vector1.begin(), vector1.end(), vector2.begin(), vector2.end());

   if (test == true)
      cout << "Matched" << endl;

   test = includes(vector1.begin(), vector1.end(), vector2.begin(), vector2.end());

   if (test == false)
      cout << "Not matched" << endl;

   return 0;

This was a basic concept about using the std:: include() function for matching elements.

Also read: Increment and Decrement Operator in C++

Leave a Reply

Your email address will not be published. Required fields are marked *