C++之lexicographical_compare
lexicographical_compare: C++ STL 泛型算法函数:用于按字典序比较两个序列。
函数申明:
/**
重载1
如果[first1, last1)按字典序列小于[first2, last2),返回true,否则返回false。
*/
template
bool lexicographical_compare( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2 );
/**
重载2
功能同重载1,增加了比较函数comp,即大小关系由comp函数确定。
*/
template < class InputIterator1, class InputIterator2,
class Compare >
bool lexicographical_compare( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
Compare comp );
For example:
View Code
1 // alg_lex_comp.cpp
2 // compile with: /EHsc
3 #include <vector>
4 #include <list>
5 #include <algorithm>
6 #include <iostream>
7
8 // Return whether second element is twice the first
9 bool twice ( int elem1, int elem2 )
10 {
11 return 2 * elem1 < elem2;
12 }
13
14 int main( )
15 {
16 using namespace std;
17 vector <int> v1, v2;
18 list <int> L1;
19 vector <int>::iterator Iter1, Iter2;
20 list <int>::iterator L1_Iter, L1_inIter;
21
22 int i;
23 for ( i = 0 ; i <= 5 ; i++ )
24 {
25 v1.push_back( 5 * i );
26 }
27 int ii;
28 for ( ii = 0 ; ii <= 6 ; ii++ )
29 {
30 L1.push_back( 5 * ii );
31 }
32
33 int iii;
34 for ( iii = 0 ; iii <= 5 ; iii++ )
35 {
36 v2.push_back( 10 * iii );
37 }
38
39 cout << "Vector v1 = ( " ;
40 for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
41 cout << *Iter1 << " ";
42 cout << ")" << endl;
43
44 cout << "List L1 = ( " ;
45 for ( L1_Iter = L1.begin( ) ; L1_Iter!= L1.end( ) ; L1_Iter++ )
46 cout << *L1_Iter << " ";
47 cout << ")" << endl;
48
49 cout << "Vector v2 = ( " ;
50 for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
51 cout << *Iter2 << " ";
52 cout << ")" << endl;
53
54 // Self lexicographical_comparison of v1 under identity
55 bool result1;
56 result1 = lexicographical_compare (v1.begin( ), v1.end( ),
57 v1.begin( ), v1.end( ) );
58 if ( result1 )
59 cout << "Vector v1 is lexicographically_less than v1." << endl;
60 else
61 cout << "Vector v1 is not lexicographically_less than v1." << endl;
62
63 // lexicographical_comparison of v1 and L2 under identity
64 bool result2;
65 result2 = lexicographical_compare (v1.begin( ), v1.end( ),
66 L1.begin( ), L1.end( ) );
67 if ( result2 )
68 cout << "Vector v1 is lexicographically_less than L1." << endl;
69 else
70 cout << "Vector v1 is lexicographically_less than L1." << endl;
71
72 bool result3;
73 result3 = lexicographical_compare (v1.begin( ), v1.end( ),
74 v2.begin( ), v2.end( ), twice );
75 if ( result3 )
76 cout << "Vector v1 is lexicographically_less than v2 "
77 << "under twice." << endl;
78 else
79 cout << "Vector v1 is not lexicographically_less than v2 "
80 << "under twice." << endl;
81 }
82 /*
83 Output:
84
85 Vector v1 = ( 0 5 10 15 20 25 )
86 List L1 = ( 0 5 10 15 20 25 30 )
87 Vector v2 = ( 0 10 20 30 40 50 )
88 Vector v1 is not lexicographically_less than v1.
89 Vector v1 is lexicographically_less than L1.
90 Vector v1 is not lexicographically_less than v2 under twice.
91 */
原文链接: https://www.cnblogs.com/cchun/archive/2012/06/10/2544018.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/52406
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!