std::equal
| 헤더 <algorithm>
|
||
template< class InputIt1, class InputIt2 >
bool equal( InputIt1 first1, InputIt1 last1,
InputIt2 first2 );
|
에 정의됨 (1) | (C++20부터 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
bool equal( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2 );
|
(2) | (C++17부터) |
template< class InputIt1, class InputIt2, class BinaryPred >
bool equal( InputIt1 first1, InputIt1 last1,
InputIt2 first2, BinaryPred p );
|
(3) | (C++20부터 constexpr) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class BinaryPred >
bool equal( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, BinaryPred p );
|
(4) | (C++17부터) |
template< class InputIt1, class InputIt2 >
bool equal( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2 );
|
(5) | (C++14부터) (C++20부터 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
bool equal( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2 );
|
(6) | (C++17부터) |
template< class InputIt1, class InputIt2, class BinaryPred >
bool equal( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, BinaryPred p );
|
(7) | (C++14부터) (C++20부터 constexpr) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class BinaryPred >
bool equal( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2, BinaryPred p );
|
(8) | (C++17부터) |
[first1, last1) 와 first2 에서 시작하는 범위가 같은지 확인합니다:
- 오버로드 (1-4)의 경우 두 번째 범위는
std::distance(first1, last1)개의 요소를 갖습니다. - 오버로드 (5-8)의 경우 두 번째 범위는
[first2,last2)입니다.
operator== 를 사용하여 비교합니다.p 를 사용하여 비교합니다.policy 에 따라 실행됩니다.true 인 경우에만 오버로드 해결에 참여합니다:
|
|
(C++20까지) |
|
|
(C++20부터) |
매개변수
| first1, last1 | - | 비교할 첫 번째 범위의 요소를 정의하는 반복자 쌍 |
| first2, last2 | - | 비교할 두 번째 범위의 요소를 정의하는 반복자 쌍 |
| policy | - | 사용할 실행 정책 |
| p | - | 요소를 같음으로 처리해야 하면 true 를 반환하는 이진 술어. 술어 함수의 서명은 다음과 동등해야 합니다:
서명에 |
| 타입 요구 사항 | ||
-InputIt1, InputIt2 은 LegacyInputIterator 의 요구 사항을 충족해야 합니다.
| ||
-ForwardIt1, ForwardIt2 은 LegacyForwardIterator 의 요구 사항을 충족해야 합니다.
| ||
-BinaryPred 은 BinaryPredicate 의 요구 사항을 충족해야 합니다.
| ||
반환값
true 를 반환합니다. 그렇지 않으면 false 를 반환합니다.std::distance(first1, last1) 와 std::distance(first2, last2) 가 같고 두 범위의 각 해당 요소가 같으면 true 를 반환합니다. 그렇지 않으면 false 를 반환합니다.복잡도
N1 을 std::distance(first1, last1) 로, N2 을 std::distance(first2, last2) 로 지정할 때:
operator== 비교.operator== 비교.p 적용.p 적용.InputIt1 와 InputIt2 가 모두 LegacyRandomAccessIterator 이고 last1 - first1 != last2 - first2 가 true 인 경우 비교가 수행되지 않습니다.operator== 비교.operator== 비교.p 적용.p 적용.예외
ExecutionPolicy 이라는 템플릿 매개변수가 있는 오버로드는 다음과 같이 오류를 보고합니다:
- 알고리즘의 일부로 호출된 함수가 예외를 던지고
ExecutionPolicy가 표준 정책 중 하나이면 std::terminate 가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현 정의입니다. - 알고리즘이 메모리 할당에 실패하면 std::bad_alloc 이 던져집니다.
가능한 구현
| equal (1) |
|---|
template<class InputIt1, class InputIt2>
constexpr //< since C++20
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
{
for (; first1 != last1; ++first1, ++first2)
if (!(*first1 == *first2))
return false;
return true;
}
|
| equal (3) |
template<class InputIt1, class InputIt2, class BinaryPred>
constexpr //< since C++20
bool equal(InputIt1 first1, InputIt1 last1,
InputIt2 first2, BinaryPred p)
{
for (; first1 != last1; ++first1, ++first2)
if (!p(*first1, *first2))
return false;
return true;
}
|
| equal (5) |
namespace detail
{
// random-access iterator implementation (allows quick range size detection)
template<class RandomIt1, class RandomIt2>
constexpr //< since C++20
bool equal(RandomIt1 first1, RandomIt1 last1, RandomIt2 first2, RandomIt2 last2,
std::random_access_iterator_tag, std::random_access_iterator_tag)
{
if (last1 - first1 != last2 - first2)
return false;
for (; first1 != last1; ++first1, ++first2)
if (!(*first1 == *first2))
return false;
return true;
}
// input iterator implementation (needs to manually compare with “last2”)
template<class InputIt1, class InputIt2>
constexpr //< since C++20
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
std::input_iterator_tag, std::input_iterator_tag)
{
for (; first1 != last1 && first2 != last2; ++first1, ++first2)
if (!(*first1 == *first2))
return false;
return first1 == last1 && first2 == last2;
}
}
template<class InputIt1, class InputIt2>
constexpr //< since C++20
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
{
details::equal(first1, last1, first2, last2,
typename std::iterator_traits<InputIt1>::iterator_category(),
typename std::iterator_traits<InputIt2>::iterator_category());
}
|
| equal (7) |
namespace detail
{
// random-access iterator implementation (allows quick range size detection)
template<class RandomIt1, class RandomIt2, class BinaryPred>
constexpr //< since C++20
bool equal(RandomIt1 first1, RandomIt1 last1,
RandomIt2 first2, RandomIt2 last2, BinaryPred p,
std::random_access_iterator_tag, std::random_access_iterator_tag)
{
if (last1 - first1 != last2 - first2)
return false;
for (; first1 != last1; ++first1, ++first2)
if (!p(*first1, *first2))
return false;
return true;
}
// input iterator implementation (needs to manually compare with “last2”)
template<class InputIt1, class InputIt2, class BinaryPred>
constexpr //< since C++20
bool equal(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, BinaryPred p,
std::input_iterator_tag, std::input_iterator_tag)
{
for (; first1 != last1 && first2 != last2; ++first1, ++first2)
if (!p(*first1, *first2))
return false;
return first1 == last1 && first2 == last2;
}
}
template<class InputIt1, class InputIt2, class BinaryPred>
constexpr //< since C++20
bool equal(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, BinaryPred p)
{
details::equal(first1, last1, first2, last2, p,
typename std::iterator_traits<InputIt1>::iterator_category(),
typename std::iterator_traits<InputIt2>::iterator_category());
}
|
참고
std::equalstd::unordered_set, std::unordered_multiset, std::unordered_map, 또는 std::unordered_multimap 의 반복자로 형성된 범위를 비교하는 데 사용해서는 안 됩니다. 두 컨테이너에 동일한 요소가 저장되어 있더라도 해당 컨테이너에서 요소가 저장되는 순서가 다를 수 있기 때문입니다.
전체 컨테이너 또는 문자열 뷰(C++17부터)를 동등성 비교할 때는 일반적으로 해당 타입에 대한 operator== 연산자가 선호됩니다.
순차 std::equal 은 단락 평가가 보장되지 않습니다. 예를 들어 두 범위의 첫 번째 요소 쌍이 같지 않더라도 나머지 요소가 비교될 수 있습니다. std::memcmp 또는 구현별 벡터화된 알고리즘으로 범위를 비교할 때 비단락 비교가 발생할 수 있습니다.
예제
다음 코드는 std::equal 를 사용하여 문자열이 회문인지 테스트합니다.
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string_view>
constexpr bool is_palindrome(const std::string_view& s)
{
return std::equal(s.cbegin(), s.cbegin() + s.size() / 2, s.crbegin());
}
void test(const std::string_view& s)
{
std::cout << std::quoted(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) |
특정 기준을 충족하는 첫 번째 요소를 찾습니다 (함수 템플릿 & 알고리즘 함수 객체) |
(C++20)(C++20)(C++20) |
|
| 두 범위를 사전식으로 비교합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
| 두 범위가 다른 첫 번째 위치를 찾습니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
| 요소 범위의 첫 번째 출현을 검색합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
(C++20) |
두 요소 집합이 동일한지 판단합니다 (알고리즘 함수 객체) |
x == y 를 구현하는 함수 객체 (클래스 템플릿) | |
| 이진 검색을 사용하여 주어진 값과 일치하는 요소의 범위를 찾습니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |