lexicographical_compare
由两个序列之间的元素比较元素确定是较小者两个。
template<class InputIterator1, class InputIterator2>
bool lexicographical_compare(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2
);
template<class InputIterator1, class InputIterator2, class BinaryPredicate>
bool lexicographical_compare(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2,
BinaryPredicate _Comp
);
参数
_First1
处理第一元素位置的输入迭代器在进行比较的第一个范围。_Last1
寻址最终元素的输入迭代器位置一将比较的第一个范围。_First2
处理第一元素位置的输入迭代器将比较的第二个范围。_Last2
寻址最终元素的输入迭代器位置一将比较的第二个范围。_Comp
用户定义的谓词函数对象定义一个元素小于另一个。 二进制谓词采用两个参数,并且在满足时返回 true,在未满足时返回 false。
返回值
true,如果第一范围比第二字典的范围小于;为 false。
备注
在序列之间存在着字典来比较元素由比较这些元素之前:
发现两个不相等的对应元素,并且,它们的比较结果采用作为比较在序列之间。
不相等未找到,但一个序列比其他具有更多元素,因此,较短的序列比更长序列视为较少。
不相等,并且未找到序列具有相同数量的元素,因此,和序列相等,然后比较结果为 false。
示例
// alg_lex_comp.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
// Return whether second element is twice the first
bool twice ( int elem1, int elem2 )
{
return 2 * elem1 < elem2;
}
int main( )
{
using namespace std;
vector <int> v1, v2;
list <int> L1;
vector <int>::iterator Iter1, Iter2;
list <int>::iterator L1_Iter, L1_inIter;
int i;
for ( i = 0 ; i <= 5 ; i++ )
{
v1.push_back( 5 * i );
}
int ii;
for ( ii = 0 ; ii <= 6 ; ii++ )
{
L1.push_back( 5 * ii );
}
int iii;
for ( iii = 0 ; iii <= 5 ; iii++ )
{
v2.push_back( 10 * iii );
}
cout << "Vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
cout << "List L1 = ( " ;
for ( L1_Iter = L1.begin( ) ; L1_Iter!= L1.end( ) ; L1_Iter++ )
cout << *L1_Iter << " ";
cout << ")" << endl;
cout << "Vector v2 = ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")" << endl;
// Self lexicographical_comparison of v1 under identity
bool result1;
result1 = lexicographical_compare (v1.begin( ), v1.end( ),
v1.begin( ), v1.end( ) );
if ( result1 )
cout << "Vector v1 is lexicographically_less than v1." << endl;
else
cout << "Vector v1 is not lexicographically_less than v1." << endl;
// lexicographical_comparison of v1 and L2 under identity
bool result2;
result2 = lexicographical_compare (v1.begin( ), v1.end( ),
L1.begin( ), L1.end( ) );
if ( result2 )
cout << "Vector v1 is lexicographically_less than L1." << endl;
else
cout << "Vector v1 is lexicographically_less than L1." << endl;
bool result3;
result3 = lexicographical_compare (v1.begin( ), v1.end( ),
v2.begin( ), v2.end( ), twice );
if ( result3 )
cout << "Vector v1 is lexicographically_less than v2 "
<< "under twice." << endl;
else
cout << "Vector v1 is not lexicographically_less than v2 "
<< "under twice." << endl;
}
要求
标头: <算法>
命名空间: std