I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingStlAlgorithmsSet

//

Returns whether each element of a range is also an element of another range

Definition

The includes() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.

Interface

#include <algorithm>
template < class InputIterator1, class InputIterator2 >
   bool includes(
      InputIterator1 first1, 
      InputIterator1 last1,
      InputIterator2 first2, 
      InputIterator2 last1
   );
template < class InputIterator1, class InputIterator2, class BinaryPredicate >
   bool includes(
      InputIterator1 first1, 
      InputIterator1 last1,
      InputIterator2 first2, 
      InputIterator2 last1,
      BinaryPredicate comp
   );

Parameters:
Parameter Description
first1 An input iterator addressing the position of the first element in the first of two sorted source ranges to be tested for whether all the elements of the second are contained in the first
last1 An input iterator addressing the position one past the last element in the first of two sorted source ranges to be tested for whether all the elements of the second are contained in the first
first2 An input iterator addressing the position of the first element in second of two consecutive sorted source ranges to be tested for whether all the elements of the second are contained in the first
last2 An input iterator addressing the position one past the last element in second of two consecutive sorted source ranges to be tested for whether all the elements of the second are contained in the first
comp User-defined predicate function object that defines sense in which one element is less than another. A binary predicate takes two arguments and returns true when satisfied and false when not satisfied

Description

Includes function tests if one sorted range ([first2, last2)) contains all the elements contained in a second sorted range ([first1, last1)), where the ordering or equivalence criterion between elements may be specified by a binary predicate.

The first version compares objects using operator<, and the second compares objects using a function object comp.

Return Value

Returns true if every element from [first2, last2) is a member of [first1, last1), otherwise returns false.

Complexity

The complexity is linear; performs at most 2 * ((last1 - first1) + (last2 - first2)) - 1 comparisons.
Example:

Example - includes algorithm
Problem
This program illustrates the use of the STL includes() algorithm (default version) to test whether the values in a second (sorted) range of integer values in a vector of integers are all present in a first (sorted) range of integer values, also in a vector of integers.
Workings
#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int main()
{
  int a1[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
  vector<int> v1(a1, a1+10);
 
  cout <<"\nHere are the contents of v1:\n";
  for (vector<int>::size_type i=0; i<v1.size(); i++)
    cout <<v1.at(i)<<" "; 
 
  int a2[] = {2, 16, 128, 512};
  vector<int> v2(a2, a2+4);
 
  cout <<"\nHere are the contents of v2:\n";
  for (vector<int>::size_type i=0; i<v2.size(); i++)
    cout <<v2.at(i)<<" "; 
 
  if (includes(v1.begin(), v1.end(), v2.begin(), v2.end()))
    cout <<"\nv1 includes v2.";
  else
    cout <<"\nv1 does not include v2.";
 
  int a3[] = {2, 16, 128, 1024};
  vector<int> v3(a3, a3+4);
 
  cout <<"\nHere are the contents of v3:\n";
  for (vector<int>::size_type i=0; i<v3.size(); i++)
    cout <<v3.at(i)<<" ";
 
  if (includes(v1.begin(), v1.end(), v3.begin(), v3.end()))
    cout <<"\nv1 includes v3.";
  else
    cout <<"\nv1 does not include v3.";
 
  return 0;
}
Solution
Output:

Here are the contents of v1:
1 2 4 8 16 32 64 128 256 512

Here are the contents of v2:
2 16 128 512

v1 includes v2.

Here are the contents of v3:
2 16 128 1024

v1 does not include v3.
References