STLでLexicoGraphical_Compareを実装するためのC++プログラム
C++関数std::alarmithm ::lexicographical_compare()は、ある範囲が辞書式に別の範囲よりも小さいかどうかをテストします。辞書式比較は、辞書で単語をアルファベット順に並べ替えるために一般的に使用される種類の比較です。
宣言
テンプレート
bool lexicographical_compare(InputIterator1 first1、InputIterator1 last1、InputIterator2 first2、InputIterator2 last2);
アルゴリズム
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
例
#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;
} 出力
v1 is less than v2. v1 is not less than v2.
-
STLにSet_Intersectionを実装するC++プログラム
2つのセットの共通部分は、両方のセットに共通する要素によってのみ形成されます。関数によってコピーされる要素は、常に同じ順序で最初のセットから取得されます。両方のセットの要素はすでに注文されている必要があります。 一般的な集合演算は-です セットユニオン 交差点を設定 対称集合の差または排他的論理和 差または減算を設定 アルゴリズム Begin Declare set vector v and iterator st. Initialize st = set_intersection (set1, set1 + n, set2, s
-
STLにSet_Differenceを実装するC++プログラム
2つのセットの違いは、2番目のセットではなく、最初のセットに存在する要素によってのみ形成されます。関数によってコピーされる要素は、常に同じ順序で最初のセットから取得されます。両方のセットの要素はすでに注文されている必要があります。 一般的な集合演算は-です セットユニオン 交差点を設定 対称集合の差または排他的論理和 差または減算を設定 アルゴリズム Begin Declare set vector v and iterator st. Initialize st = set_difference (set1, set1 + n,