Computer >> Máy Tính >  >> Lập trình >> C ++

Chương trình C ++ để triển khai LexicoGraphical_Compare trong STL

Hàm C ++ std ::giải thuật ::lexicographical_compare () kiểm tra xem một phạm vi từ vựng có nhỏ hơn phạm vi khác hay không. So sánh từ vựng là loại so sánh thường được sử dụng để sắp xếp các từ theo thứ tự bảng chữ cái trong từ điển.

Tuyên bố

mẫu

bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);

Thuật toán

Begin
   result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end())
   if (result == true)
      Print v1 is less than v2
   result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end())
   if (result == false)
      Print v1 is not less than v2
End

Ví dụ

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main(void) {
   //initialization of v1 and v2
   vector<string> v1 = {"One", "Two", "Three"};
   vector<string> v2 = {"one", "two", "three"};
   bool result;
   result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end());
   if (result == true)
      cout << "v1 is less than v2." << endl;
      v1[0] = "two";
      result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end());
   if (result == false)
      cout << "v1 is not less than v2." << endl;
   return 0;
}

Đầu ra

v1 is less than v2.
v1 is not less than v2.