|
includes
function template
<algorithm>
template <class InputIterator1, class InputIterator2>
bool includes ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2 );
template <class InputIterator1, class InputIterator2, class Compare>
bool includes ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2, Compare comp );
Test whether sorted range includes another sorted range
Returns true if all the elements in the range [first2,last2) are equivalent to some element in [first1,last1). The comparison uses either operator< for the first version, or comp for the second to test this; The value of an element, a, is equivalent to another one, b, when (!a<b && !b<a) or (!comp(a,b) && !comp(b,a)).
For the function to yield the expected result, the elements in the ranges shall be already ordered according to the same strict weak ordering criterion (operator< or comp).
The behavior of this function template is equivalent to:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
template <class InputIterator1, class InputIterator2>
bool includes ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2 )
{
while (first1!=last1)
{
if (*first2<*first1) break;
else if (*first1<*first2) ++first1;
else { ++first1; ++first2; }
if (first2==last2) return true;
}
return false;
}
|
Parameters
- first1, last1
- Input iterators to the initial and final positions of the first sequence (which is tested on whether it contains the second sequence). The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.
- first2, last2
- Input iterators to the initial and final positions of the second sequence (which is tested on wheter it is contained in the first sequence). The range used is [first2,last2).
- comp
- Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.
Return value
true if every element in the range [first2,last2) is contained in the range [first1,last1), false otherwise.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
// includes algorithm example
#include <iostream>
#include <algorithm>
using namespace std;
bool myfunction (int i, int j) { return i<j; }
int main () {
int container[] = {5,10,15,20,25,30,35,40,45,50};
int continent[] = {40,30,20,10};
sort (container,container+10);
sort (continent,continent+4);
// using default comparison:
if ( includes(container,container+10,continent,continent+4) )
cout << "container includes continent!" << endl;
// using myfunction as comp:
if ( includes(container,container+10,continent,continent+4, myfunction) )
cout << "container includes continent!" << endl;
return 0;
}
|
Output:
container includes continent!
container includes continent!
|
Complexity
At most, performs 2*(count1+count2)-1 comparisons or applications of comp (where countX is the distance between firstX and lastX).
See also
search | Find subsequence in range (function template) |
find_end | Find last subsequence in range (function template) |
equal | Test whether the elements in two ranges are equal (function template) |
|