std::equal
| ヘッダ <algorithm> で定義
|
||
| (1) | ||
template< class InputIt1, class InputIt2 > bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2 ); |
(C++20未満) | |
template< class InputIt1, class InputIt2 > constexpr bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2 ); |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > bool equal( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 ); |
(2) | (C++17以上) |
| (3) | ||
template< class InputIt1, class InputIt2, class BinaryPredicate > bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p ); |
(C++20未満) | |
template< class InputIt1, class InputIt2, class BinaryPredicate > constexpr bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p ); |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate > bool equal( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, BinaryPredicate p ); |
(4) | (C++17以上) |
| (5) | ||
template< class InputIt1, class InputIt2 > bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2 ); |
(C++14以上) (C++20未満) |
|
template< class InputIt1, class InputIt2 > constexpr bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2 ); |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > bool equal( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2 ); |
(6) | (C++17以上) |
| (7) | ||
template< class InputIt1, class InputIt2, class BinaryPredicate > bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPredicate p ); |
(C++14以上) (C++20未満) |
|
template< class InputIt1, class InputIt2, class BinaryPredicate > constexpr bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPredicate p ); |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate > bool equal( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2, BinaryPredicate p ); |
(8) | (C++17以上) |
[first1, last1) が範囲 [first2, first2 + (last1 - first1)) と等しければ true、そうでなければ false を返します。[first1, last1) が範囲 [first2, last2) と等しければ true、そうでなければ false を返します。policy に従って実行されます。 このオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します。2つの範囲が同じ個数の要素を持ち、範囲 [first1,last1) 内のすべてのイテレータ i について *i が *(first2 + (i - first1)) と等しければ、2つの範囲は等しいとみなされます。 オーバーロード (1,2,5,6) は2つの要素が等しいかどうかを判定するために operator== を使用し、オーバーロード (3,4,7,8) は指定された二項述語を使用します。
引数
| first1, last1 | - | 1つめの比較する要素の範囲 |
| first2, last2 | - | 2つめの比較する要素の範囲 |
| policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
| p | - | 要素が等しいと扱われるべき場合に true を返す二項述語。 述語関数のシグネチャは以下と同等なものであるべきです。
シグネチャが |
| 型の要件 | ||
-InputIt1, InputIt2 は LegacyInputIterator の要件を満たさなければなりません。
| ||
-ForwardIt1, ForwardIt2 は LegacyForwardIterator の要件を満たさなければなりません。
| ||
戻り値
[first1, last1) の長さが範囲 [first2, last2) の長さと等しくなければ false を返します。2つの範囲の要素が等しければ true を返します。
そうでなければ false を返します。
ノート
std::equal は、 std::unordered_set、 std::unordered_multiset、 std::unordered_map または std::unordered_multimap のイテレータによって形成された範囲を比較するためには、使用するべきではありません。 これらのコンテナは、2つのコンテナが同じ要素を格納していても、その要素が格納されている順序は異なる可能性があるためです。
コンテナ全体の等しさを比較するときは、通常、対応するコンテナの operator== が推奨されます。
計算量
last1 - first1 回の述語の適用。last1 - first1, last2 - first2) 回の述語の適用。InputIt1 と InputIt2 が LegacyRandomAccessIterator の要件を満たし、 last1 - first1 != last2 - first2 である場合、述語の適用は行われません (いかなる要素も見ずにサイズの不一致が検出されます)。
例
テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicyが標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicyについては、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
実装例
| 1つめのバージョン |
|---|
template<class InputIt1, class InputIt2>
bool equal(InputIt1 first1, InputIt1 last1,
InputIt2 first2)
{
for (; first1 != last1; ++first1, (void)++first2) {
if (!(*first1 == *first2)) {
return false;
}
}
return true;
}
|
| 2つめのバージョン |
template<class InputIt1, class InputIt2, class BinaryPredicate>
bool equal(InputIt1 first1, InputIt1 last1,
InputIt2 first2, BinaryPredicate p)
{
for (; first1 != last1; ++first1, (void)++first2) {
if (!p(*first1, *first2)) {
return false;
}
}
return true;
}
|
例
以下のコードは文字列が回文かどうかを判定するために equal() を使用します。
#include <algorithm>
#include <iostream>
#include <string>
bool is_palindrome(const std::string& s)
{
return std::equal(s.begin(), s.begin() + s.size()/2, s.rbegin());
}
void test(const std::string& s)
{
std::cout << "\"" << s << "\" "
<< (is_palindrome(s) ? "is" : "is not")
<< " a palindrome\n";
}
int main()
{
test("radar");
test("hello");
}
出力:
"radar" is a palindrome
"hello" is not a palindrome
関連項目
(C++11) |
一定の基準を満たす最初の要素を探します (関数テンプレート) |
| ある範囲が別の範囲より辞書的に小さいかどうか調べます (関数テンプレート) | |
| 2つの範囲が異なる最初の位置を探します (関数テンプレート) | |
| 指定範囲の要素に対して検索を行います (関数テンプレート) |