Cpp-standard-library-cpp-algorithm-lexicographical-compare-v2
提供:Dev Guides
C ++アルゴリズムライブラリ-lexicographical_compare()関数
説明
- C ++関数* std
- algorithm :: lexicographical_compare()*は、ある範囲が別の範囲より辞書式に小さいかどうかをテストします。 辞書編集比較は、辞書の単語をアルファベット順に並べ替えるために一般的に使用される比較の一種です。
宣言
- 以下は、std
- algorithmヘッダーからのstd :: algorithm :: lexicographical_compare()関数の宣言です。
C 98
template <class InputIterator1, class InputIterator2, class Compare>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,Compare comp);
パラメーター
- first1 -最初のシーケンスの初期位置に反復子を入力します。
- last1 -最初のシーケンスの最終位置に反復子を入力します。
- first2 -2番目のシーケンスの初期位置に反復子を入力します。
- last2 -2番目のシーケンスの最終位置に反復子を入力します。
- comp -2つの引数を受け取り、boolを返すバイナリ関数。
戻り値
1つの範囲が辞書的に2番目に小さい場合はtrueを返し、そうでない場合はfalseを返します。
例外
`+ binary function +`またはイテレータの操作が例外をスローした場合、例外をスローします。
無効なパラメータは未定義の動作を引き起こすことに注意してください。
時間の複雑さ
- 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;
bool ignore_case(string s1, string s2) {
return true;
}
int main(void) {
vector<string> v1 = {"one", "two"};
vector<string> v2 = {"One", "Two", "Three"};
bool result;
result = lexicographical_compare(v1.begin(), v1.end(),
v2.begin(), v2.end());
if (result == false)
cout << "v1 is not less than v2." << endl;
result = lexicographical_compare(v1.begin(), v1.end(),
v2.begin(), v2.end(), ignore_case);
if (result == true)
cout << "v1 is less than v2." << endl;
return 0;
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
v1 is not less than v2.
v1 is less than v2.