zoukankan      html  css  js  c++  java
  • C++之lexicographical_compare

    C++之lexicographical_compare

    lexicographical_compare: C++ STL 泛型算法函数:用于按字典序比较两个序列。

    函数申明:

    /**

    重载1

    如果[first1, last1)按字典序列小于[first2, last2),返回true,否则返回false。

    */

    template <class InputIterator1, class InputIterator2 >

    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 */
  • 相关阅读:
    POJ3159 Candies —— 差分约束 spfa
    POJ1511 Invitation Cards —— 最短路spfa
    POJ1860 Currency Exchange —— spfa求正环
    POJ3259 Wormholes —— spfa求负环
    POJ3660 Cow Contest —— Floyd 传递闭包
    POJ3268 Silver Cow Party —— 最短路
    POJ1797 Heavy Transportation —— 最短路变形
    POJ2253 Frogger —— 最短路变形
    POJ1759 Garland —— 二分
    POJ3685 Matrix —— 二分
  • 原文地址:https://www.cnblogs.com/cchun/p/2544018.html
Copyright © 2011-2022 走看看