std::lexicographical_compare
提供: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| ヘッダ <algorithm> で定義
|
||
| (1) | ||
template< class InputIt1, class InputIt2 > bool lexicographical_compare( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2 ); |
(C++20未満) | |
template< class InputIt1, class InputIt2 > constexpr bool lexicographical_compare( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2 ); |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > bool lexicographical_compare( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2 ); |
(2) | (C++17以上) |
| (3) | ||
template< class InputIt1, class InputIt2, class Compare > bool lexicographical_compare( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp ); |
(C++20未満) | |
template< class InputIt1, class InputIt2, class Compare > constexpr bool lexicographical_compare( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp ); |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class Compare > bool lexicographical_compare( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2, Compare comp ); |
(4) | (C++17以上) |
1つめの範囲 [first1, last1) が2つめの範囲 [first2, last2) より辞書的に小さいかどうか調べます。
1) 要素は
operator< を用いて比較されます。3) 要素は指定された二項比較関数
comp を用いて比較されます。2,4) (1,3) と同じですが、
policy に従って実行されます。 このオーバーロードは、std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します。辞書的な比較は以下の性質を持つ操作です。
- 2つの範囲が要素単位で比較されます。
- 一致しない最初の要素が、範囲が他方より辞書的に小さいまたは大きいかどうかを定義します。
- 一方の範囲が他方の接頭辞である場合、短い方の範囲は他方より辞書的に小さくなります。
- 2つの範囲が同等な要素を持ち同じ長さである場合、その範囲は辞書的に等しくなります。
- 空の範囲は空でない範囲より辞書的に小さくなります。
- 2つの空の範囲は辞書的に等しくなります。
引数
| first1, last1 | - | 1つめの調べる要素の範囲 |
| first2, last2 | - | 2つめの調べる要素の範囲 |
| policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
| comp | - | 第1引数が第2引数より小さい場合に true を返す、比較関数オブジェクト (Compare の要件を満たすオブジェクト)。比較関数のシグネチャは以下と同等であるべきです。
シグネチャが |
| 型の要件 | ||
-InputIt1, InputIt2 は LegacyInputIterator の要件を満たさなければなりません。
| ||
-ForwardIt1, ForwardIt2 は LegacyForwardIterator の要件を満たさなければなりません。
| ||
戻り値
1つめの範囲が2つめの範囲より辞書的に小さい場合は true。
計算量
多くとも 2·min(N1, N2) 回の比較操作の適用、ただし N1 = std::distance(first1, last1)、 N2 = std::distance(first2, last2) です。
例外
テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicyが標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicyについては、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
実装例
| 1つめのバージョン |
|---|
template<class InputIt1, class InputIt2>
bool lexicographical_compare(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2)
{
for ( ; (first1 != last1) && (first2 != last2); ++first1, (void) ++first2 ) {
if (*first1 < *first2) return true;
if (*first2 < *first1) return false;
}
return (first1 == last1) && (first2 != last2);
}
|
| 2つめのバージョン |
template<class InputIt1, class InputIt2, class Compare>
bool lexicographical_compare(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
Compare comp)
{
for ( ; (first1 != last1) && (first2 != last2); ++first1, (void) ++first2 ) {
if (comp(*first1, *first2)) return true;
if (comp(*first2, *first1)) return false;
}
return (first1 == last1) && (first2 != last2);
}
|
例
Run this code
#include <algorithm>
#include <iostream>
#include <vector>
#include <random>
int main()
{
std::vector<char> v1 {'a', 'b', 'c', 'd'};
std::vector<char> v2 {'a', 'b', 'c', 'd'};
std::mt19937 g{std::random_device{}()};
while (!std::lexicographical_compare(v1.begin(), v1.end(),
v2.begin(), v2.end())) {
for (auto c : v1) std::cout << c << ' ';
std::cout << ">= ";
for (auto c : v2) std::cout << c << ' ';
std::cout << '\n';
std::shuffle(v1.begin(), v1.end(), g);
std::shuffle(v2.begin(), v2.end(), g);
}
for (auto c : v1) std::cout << c << ' ';
std::cout << "< ";
for (auto c : v2) std::cout << c << ' ';
std::cout << '\n';
}
出力例:
a b c d >= a b c d
d a b c >= c b d a
b d a c >= a d c b
a c d b < c d a b
関連項目
| 2つの要素集合が同じかどうか調べます (関数テンプレート) |