當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ lexicographical_compare()用法及代碼示例


C++ STL提供了許多實用程序來解決基本的生活方麵的問題。比較值始終是必要的,但有時我們還需要比較字符串。因此,本文旨在說明有關允許比較字符串的“lexicographical_compare()”。此函數在“algorithm”標頭中定義。它有兩個實現。

實施1: lexicographical_compare(iter1 beg1,iter1 end1,iter2 beg2,iter2 end2)

Template:
template 
  bool lexicographical_compare(iter1 beg1, iter1 end1, 
                               iter2 beg2, iter2 end2)
{
  while (beg1!=end1)
  {
    if (beg2==end2 || *beg2<*beg1) return false;
    else if (*beg1<*beg2) return true;
    ++beg1; ++beg2;
  }
  return (beg2!=end2);
}
參數:
beg1: Input iterator to initial position of first sequence.
end1: Input iterator to final position of first sequence.

beg2: Input iterator to initial position of second sequence.
end2: Input iterator to final position of second sequence.

返回值:
Returns a boolean true, if range1 is strictly lexicographically 
smaller than range2 else returns a false.

// C++ code to demonstrate the working of 
// lexicographical_compare(), implementation 1 
#include <iostream> 
#include <algorithm> // for lexicographical_compare() 
using namespace std; 
  
int main() 
{ 
    // initializing char arrays 
    char one[] = "geeksforgeeks"; 
    char two[] = "gfg"; 
  
    // using lexicographical_compare for checking 
    // is "one" is less than "two" 
    if (lexicographical_compare(one, one + 13, two, two + 3))  
        cout << "geeksforgeeks is lexicographically less "
                                                 "than gfg"; 
    else 
        cout << "geeksforgeeks is not lexicographically "
                                         "less than gfg";     
}

輸出:


geeksforgeeks is lexicographically less than gfg

實施2: lexicographical_compare(iter1 beg1,iter1 end1,iter2 beg2,iter2 end2,比較comp)

Template:
template 
  bool lexicographical_compare(iter1 beg1, iter1 end1, 
                               iter2 beg2, iter2 end2)
{
  while (beg1!=end1)
  {
    if (beg2==end2 || *beg2<*beg1) return false;
    else if (*beg1<*beg2) return true;
    ++beg1; ++beg2;
  }
  return (beg2!=end2);
}
參數:
beg1: Input iterator to initial position of first sequence.
end1: Input iterator to final position of first sequence.

beg2: Input iterator to initial position of second sequence.
end2: Input iterator to final position of second sequence.

comp: The comparator function that returns a boolean
true/false of the each elements compared. This function 
accepts two arguments. This can be function pointer or 
function object and cannot change values.

返回值:
Returns a boolean true, if range1 is strictly lexicographically smaller 
than range2 else returns a false.

// C++ code to demonstrate the working of 
// lexicographical_compare(), implementation 2 
  
#include <iostream> 
#include <algorithm> // for lexicographical_compare() 
using namespace std; 
  
// helper function to convert all into lower case:
bool comp(char s1, char s2) 
{ 
    return tolower(s1) < tolower(s2); 
} 
  
int main() 
{ 
    // initializing char arrays 
    char one[] = "geeksforgeeks"; 
    char two[] = "Gfg"; 
  
    // using lexicographical_compare for checking 
    // is "one" is less than "two" 
    // returns false as "g" has larger ASCII value than "G" 
    if (lexicographical_compare(one, one + 13, two, two + 3))  
        cout << "geeksforgeeks is lexicographically less "
                                             "than Gfg\n"; 
    else 
        cout << "geeksforgeeks is not lexicographically "
                                        "less than Gfg\n"; 
      
  
    // using lexicographical_compare for checking 
    // is "one" is less than "two" 
    // returns true this time as all converted into lowercase 
    if (lexicographical_compare(one, one + 13, two, two + 3, comp)) { 
        cout << "geeksforgeeks is lexicographically less  "; 
        cout << "than Gfg( case-insensitive )"; 
  
    } else { 
        cout << "geeksforgeeks is not lexicographically less "; 
        cout << "than Gfg( case-insensitive )"; 
    } 
}

輸出:

geeksforgeeks is not lexicographically less than Gfg
geeksforgeeks is lexicographically less than Gfg( case-insensitive )

可能的應用:比較字符串通常可以在字典中使用,在字典中我們需要按字典順序放置單詞。這樣的示例可以是在給定的一組單詞中找到在字典中排在第一位的單詞。

// C++ code to demonstrate the application of  
// lexicographical_compare() 
#include<bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // initializing char arrays 
    char list[][100]={ 
        {'a','b','a','c','u','s'}, 
        {'a','p','p','l','e'}, 
        {'c','a','r'}, 
        {'a','b','b','a'} 
    }; 
      
    char min[100] = "zzzzzz"; 
      
    // using lexicographical_compare for checking  
    // the smallest 
    for (int i=0; i<4; i++) 
        if( lexicographical_compare(list[i], list[i] 
                + strlen(list[i]), min, min+strlen(min)))        
         strcpy(min,list[i]); 
      
    // prints "abacus" 
    cout << "The smallest string is:"; 
    for(int i = 0; min[i]!='\0'; i++)     
        cout<<min[i];      
}

輸出:

The smallest string is:abacus



相關用法


注:本文由純淨天空篩選整理自 lexicographical_compare() in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。