function template
<algorithm>
std::includestemplate <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 the sorted range[first1,last1)
contains all the elements in the sorted range [first2,last2)
.
The elements are compared using operator<
for the first version, and comp for the second. Two elements, a and b are considered equivalent if (!(a<b) && !(b<a))
or if (!comp(a,b) && !comp(b,a))
.
The elements in the range shall already be ordered according to this same criterion (operator<
or comp).
The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
template <class InputIterator1, class InputIterator2>
bool includes (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2)
{
while (first2!=last2) {
if ( (first1==last1) || (*first2<*first1) ) return false;
if (!(*first1<*first2)) ++first2;
++first1;
}
return true;
}
[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)
.
bool
. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines.
true
if every element in the range [first2,last2)
is contained in the range [first1,last1)
, false
otherwise.
If [first2,last2)
is an empty range, the result is unspecified.
If [first2,last2)
is an empty range, the function returns true
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// includes algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::includes, std::sort
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};
std::sort (container,container+10);
std::sort (continent,continent+4);
// using default comparison:
if ( std::includes(container,container+10,continent,continent+4) )
std::cout << "container includes continent!\n";
// using myfunction as comp:
if ( std::includes(container,container+10,continent,continent+4, myfunction) )
std::cout << "container includes continent!\n";
return 0;
}
container includes continent! container includes continent!
2*(count1+count2)-1
comparisons (where countX is the distance between firstX and lastX).
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4