includes |
Search for one sequence in another sequence.
Search for one sequence of values
in another sequence of values. Return true if every
element in [ first2...last2 ) is in the sequence [
first1...last1 ). The first version assumes that both sequences are already
sorted using operator< . The second version
assumes that both sequences are already sorted using compare
.
#include <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 compare
);
Time complexity is linear. Space complexity is constant.
#include <iostream>
#include <algorithm>
int numbers1[ 5 ] = { 1, 2, 3, 4, 5 };
int numbers2[ 5 ] = { 1, 2, 4, 8, 16 };
int numbers3[ 2 ] = { 4, 8 };
void
main()
{
if ( includes( numbers1, numbers1 + 5, numbers3, numbers3 + 2 ) )
cout << "numbers1 includes numbers3\n";
else
cout << "numbers1 does not include numbers3\n";
if ( includes( numbers2, numbers2 + 5, numbers3, numbers3 + 2 ) )
cout << "numbers2 includes numbers3\n";
else
cout << "numbers2 does not include numbers3\n";
}
numbers1 does not include numbers3
numbers2 includes numbers3
#include <iostream>
#include <algorithm>
#include <vector>
void
main()
{
vector< int > v1( 10 );
vector< int > v2( 3 );
size_t i;
for ( i = 0; i < v1.size(); ++i )
v1[ i ] = i;
if ( includes( v1.begin(), v1.end(), v2.begin(), v2.end() ))
cout << "v1 includes v2\n";
else
cout << "v1 does not include v2\n";
for ( i = 0; i < v2.size(); ++i )
v2[ i ] = i + 3;
if ( includes( v1.begin(), v1.end(), v2.begin(), v2.end() ))
cout << "v1 includes v2\n";
else
cout << "v1 does not include v2\n";
}
v1 does not include v2
v1 includes v2
#include <iostream>
#include <string.h>
#include <algorithm>
#include <vector>
bool
compare_strings( const char* s1_, const char* s2_ )
{
return ::strcmp( s1_, s2_ ) < 0 ? 1 : 0;
}
char* names[] = { "Todd", "Mike", "Graham", "Jack", "Brett"};
void
main()
{
const unsigned nameSize = sizeof( names )/sizeof( names[ 0 ] );
vector< char* > v1( nameSize );
for ( size_t i = 0; i < v1.size(); ++i )
v1[ i ] = names[ i ];
vector< char* > v2( 2 );
v2[ 0 ] = "foo";
v2[ 1 ] = "bar";
sort( v1.begin(), v1.end(), compare_strings );
sort( v2.begin(), v2.end(), compare_strings );
bool inc = includes
(
v1.begin(),
v1.end(),
v2.begin(),
v2.end(),
compare_strings
);
if ( inc )
cout << "v1 includes v2\n";
else
cout << "v1 does not include v2\n";
v2[ 0 ] = "Brett";
v2[ 1 ] = "Todd";
inc = includes
(
v1.begin(),
v1.end(),
v2.begin(),
v2.end(),
compare_strings
);
if ( inc )
cout << "v1 includes v2\n";
else
cout << "v1 does not include v2\n";
}
v1 does not include v2
v1 includes v2
Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.