Cpp-standard-library-cpp-algorithm-lexicographical-compare-v1

提供:Dev Guides
移動先:案内検索

C ++アルゴリズムライブラリ-lexicographical_compare()関数

説明

C ++関数* std
algorithm :: lexicographical_compare()*は、ある範囲が別の範囲より辞書式に小さいかどうかをテストします。 辞書編集比較は、辞書の単語をアルファベット順に並べ替えるために一般的に使用される比較の一種です。

宣言

以下は、std
algorithmヘッダーからのstd :: algorithm :: lexicographical_compare()関数の宣言です。

C 98

template <class InputIterator1, class InputIterator2>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
   InputIterator2 first2, InputIterator2 last2);

パラメーター

  • first1 -最初のシーケンスの初期位置に反復子を入力します。
  • last1 -最初のシーケンスの最終位置に反復子を入力します。
  • first2 -2番目のシーケンスの初期位置に反復子を入力します。
  • last2 -2番目のシーケンスの最終位置に反復子を入力します。

戻り値

1つの範囲が辞書的に2番目に小さい場合はtrueを返し、そうでない場合はfalseを返します。

例外

要素比較またはイテレータの操作が例外をスローした場合、例外をスローします。

無効なパラメータは未定義の動作を引き起こすことに注意してください。

時間の複雑さ

2 * min(N1、N2)、N1 = std
distance(first1、last1)およびN2 = std :: distance(first2、last2)。

次の例は、std
algorithm :: lexicographical_compare()関数の使用法を示しています。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int main(void) {
   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;
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

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