includes
测试已排序的范围是否包含在秒排序的范围包含的所有元素,该元素之间的排序或等效性标准可以由二进制谓词指定。
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
);
参数
_First1
解决输入的迭代器第一个元素的位置在第一个排序源范围要测试的所有元素的第二是否在第一个包含。_Last1
解决输入的迭代器通过最后一个元素的位置一在第一个排序源范围要测试的所有元素的第二是否在第一个包含。_First2
解决输入的迭代器第一个元素的位置在第二两个连续的排序的源范围要测试的所有元素的第二是否在第一个包含。_Last2
解决输入的迭代器通过最后一个元素的位置一在第二两个连续的排序的源范围要测试的所有元素的第二是否在第一个包含。_Comp
定义含义的用户定义的谓词函数对象哪个元素比另一个小于。 二进制谓词采用两个参数并返回 true,在满足和 false,在未满足。
返回值
true,如果第一个排序的范围在第二个排序的范围包含任何元素;否则,false。
备注
另一种观点测试是它确定了第二个源范围是否是第一个源范围的子集。
引用的排序的源范围必须是有效的;所有指针必须dereferenceable,因此,在每个序列中,最后位置必须是可访问的从开始按增量。
必须每个位置排序的源范围,当给算法的应用程序的前提条件包括与排序的相同与与将算法使用排序合并的大小。
算法不修改源范围 merge。
输入迭代器的值类型需要小于可将排序,因此,命名两个组件,可以确定为它们是等效的(来讲都比其他不小于)或一个比其他小于。 这将导致排序在非等价的元素之间。 更确切地说,该算法测试在第一个排序范围的所有元素都指定的二进制谓词下是否具有相同顺序对那些在第二个排序的大小。
算法的复杂是线性使用最多2个* (_Last1 – _First1) (_Last2 – _First2) – 1非空源范围的比较。
示例
// alg_includes.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional> // For greater<int>( )
#include <iostream>
// Return whether modulus of elem1 is less than modulus of elem2
bool mod_lesser (int elem1, int elem2 )
{
if ( elem1 < 0 )
elem1 = - elem1;
if ( elem2 < 0 )
elem2 = - elem2;
return elem1 < elem2;
}
int main( )
{
using namespace std;
vector <int> v1a, v1b;
vector <int>::iterator Iter1a, Iter1b;
// Constructing vectors v1a & v1b with default less-than ordering
int i;
for ( i = -2 ; i <= 4 ; i++ )
{
v1a.push_back( i );
}
int ii;
for ( ii =-2 ; ii <= 3 ; ii++ )
{
v1b.push_back( ii );
}
cout << "Original vector v1a with range sorted by the\n "
<< "binary predicate less than is v1a = ( " ;
for ( Iter1a = v1a.begin( ) ; Iter1a != v1a.end( ) ; Iter1a++ )
cout << *Iter1a << " ";
cout << ")." << endl;
cout << "Original vector v1b with range sorted by the\n "
<< "binary predicate less than is v1b = ( " ;
for ( Iter1b = v1b.begin ( ) ; Iter1b != v1b.end ( ) ; Iter1b++ )
cout << *Iter1b << " ";
cout << ")." << endl;
// Constructing vectors v2a & v2b with ranges sorted by greater
vector <int> v2a ( v1a ) , v2b ( v1b );
vector <int>::iterator Iter2a, Iter2b;
sort ( v2a.begin ( ) , v2a.end ( ) , greater<int> ( ) );
sort ( v2b.begin ( ) , v2b.end ( ) , greater<int> ( ) );
v2a.pop_back ( );
cout << "Original vector v2a with range sorted by the\n "
<< "binary predicate greater is v2a = ( " ;
for ( Iter2a = v2a.begin ( ) ; Iter2a != v2a.end ( ) ; Iter2a++ )
cout << *Iter2a << " ";
cout << ")." << endl;
cout << "Original vector v2b with range sorted by the\n "
<< "binary predicate greater is v2b = ( " ;
for ( Iter2b = v2b.begin ( ) ; Iter2b != v2b.end ( ) ; Iter2b++ )
cout << *Iter2b << " ";
cout << ")." << endl;
// Constructing vectors v3a & v3b with ranges sorted by mod_lesser
vector <int> v3a ( v1a ), v3b ( v1b ) ;
vector <int>::iterator Iter3a, Iter3b;
reverse (v3a.begin( ), v3a.end( ) );
v3a.pop_back ( );
v3a.pop_back ( );
sort ( v3a.begin ( ) , v3a.end ( ) , mod_lesser );
sort ( v3b.begin ( ) , v3b.end ( ) , mod_lesser );
cout << "Original vector v3a with range sorted by the\n "
<< "binary predicate mod_lesser is v3a = ( " ;
for ( Iter3a = v3a.begin ( ) ; Iter3a != v3a.end ( ) ; Iter3a++ )
cout << *Iter3a << " ";
cout << ")." << endl;
cout << "Original vector v3b with range sorted by the\n "
<< "binary predicate mod_lesser is v3b = ( " ;
for ( Iter3b = v3b.begin ( ) ; Iter3b != v3b.end ( ) ; Iter3b++ )
cout << *Iter3b << " ";
cout << ")." << endl;
// To test for inclusion under an asscending order
// with the default binary predicate less <int> ( )
bool Result1;
Result1 = includes ( v1a.begin ( ) , v1a.end ( ) ,
v1b.begin ( ) , v1b.end ( ) );
if ( Result1 )
cout << "All the elements in vector v1b are "
<< "contained in vector v1a." << endl;
else
cout << "At least one of the elements in vector v1b "
<< "is not contained in vector v1a." << endl;
// To test for inclusion under descending
// order specify binary predicate greater<int>( )
bool Result2;
Result2 = includes ( v2a.begin ( ) , v2a.end ( ) ,
v2b.begin ( ) , v2b.end ( ) , greater <int> ( ) );
if ( Result2 )
cout << "All the elements in vector v2b are "
<< "contained in vector v2a." << endl;
else
cout << "At least one of the elements in vector v2b "
<< "is not contained in vector v2a." << endl;
// To test for inclusion under a user
// defined binary predicate mod_lesser
bool Result3;
Result3 = includes ( v3a.begin ( ) , v3a.end ( ) ,
v3b.begin ( ) , v3b.end ( ) , mod_lesser );
if ( Result3 )
cout << "All the elements in vector v3b are "
<< "contained under mod_lesser in vector v3a."
<< endl;
else
cout << "At least one of the elements in vector v3b is "
<< " not contained under mod_lesser in vector v3a."
<< endl;
}
要求
标头: <algorithm>
命名空间: std