Namespaces
Variants

std::ranges:: equal

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
헤더 파일에 정의됨 <algorithm>
호출 서명
template < std:: input_iterator I1, std:: sentinel_for < I1 > S1,

std:: input_iterator I2, std:: sentinel_for < I2 > S2,
class Pred = ranges:: equal_to ,
class Proj1 = std:: identity , class Proj2 = std:: identity >
requires std:: indirectly_comparable < I1, I2, Pred, Proj1, Proj2 >
constexpr bool
equal ( I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = { } ,

Proj1 proj1 = { } , Proj2 proj2 = { } ) ;
(1) (C++20 이후)
template < ranges:: input_range R1, ranges:: input_range R2,

class Pred = ranges:: equal_to ,
class Proj1 = std:: identity , class Proj2 = std:: identity >
requires std:: indirectly_comparable < ranges:: iterator_t < R1 > , ranges:: iterator_t < R2 > ,
Pred, Proj1, Proj2 >
constexpr bool

equal ( R1 && r1, R2 && r2, Pred pred = { } , Proj1 proj1 = { } , Proj2 proj2 = { } ) ;
(2) (C++20 이후)
1) 범위 [ first1 , last1 ) 의 투영된 값들이 범위 [ first2 , last2 ) 의 투영된 값들과 같으면 true 를 반환하고, 그렇지 않으면 false 를 반환합니다.
2) (1) 과 동일하지만, r 을 소스 범위로 사용하며, 마치 ranges:: begin ( r ) first 로, ranges:: end ( r ) last 로 사용하는 것과 같습니다.

두 범위는 요소의 개수가 같고, 모든 대응하는 투영된 요소 쌍이 pred 를 만족하는 경우 동일한 것으로 간주됩니다. 즉, std:: invoke ( pred, std:: invoke ( proj1, * first1 ) , std:: invoke ( proj2, * first2 ) ) 가 두 범위의 모든 대응하는 요소 쌍에 대해 true 를 반환합니다.

이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:

목차

매개변수

first1, last1 - 비교할 첫 번째 범위 를 정의하는 반복자-감시자 쌍
r1 - 비교할 첫 번째 요소 범위
first2, last2 - 비교할 두 번째 범위 를 정의하는 반복자-감시자 쌍
r2 - 비교할 두 번째 요소 범위
pred - 투영된 요소에 적용할 이항 조건자
proj1 - 첫 번째 요소 범위에 적용할 투영
proj2 - 두 번째 요소 범위에 적용할 투영

반환값

범위 [ first1 , last1 ) 의 길이가 범위 [ first2 , last2 ) 의 길이와 같지 않으면 false 를 반환합니다.

두 범위의 요소들이 투영 후 동일하다면, true 를 반환합니다.

그렇지 않으면 false 를 반환합니다.

참고 사항

ranges::equal std::unordered_set , std::unordered_multiset , std::unordered_map , 또는 std::unordered_multimap 의 반복자들로 형성된 범위들을 비교하는 데 사용되어서는 안 됩니다. 왜냐하면 두 컨테이너가 동일한 요소들을 저장하더라도 이러한 컨테이너들 내에서 요소들이 저장되는 순서가 다를 수 있기 때문입니다.

전체 컨테이너나 문자열 뷰를 동등성 비교할 때는, operator == 를 해당 타입에 대해 사용하는 것이 일반적으로 선호됩니다.

ranges::equal 는 단락 평가가 보장되지 않습니다. 예를 들어, 두 범위의 첫 번째 요소 쌍이 서로 같지 않다고 비교되더라도 나머지 요소들도 비교될 수 있습니다. 이러한 비단락 비교는 범위들이 std::memcmp 또는 구현체별 벡터화 알고리즘으로 비교될 때 발생할 수 있습니다.

복잡도

최대 min ( last1 - first1, last2 - first2 ) 번의 술어 및 해당 투영 함수 적용.

그러나 S1 S2 가 모두 각각의 반복자에 대해 std::sized_sentinel_for 를 모델링하고, last1 - first1 ! = last2 - first2 인 경우, 술어(predicate)의 적용이 전혀 이루어지지 않습니다 (크기 불일치가 요소들을 살펴보지 않고도 감지됩니다).

가능한 구현

struct equal_fn
{
  template<std::input_iterator I1, std::sentinel_for<I1> S1,
           std::input_iterator I2, std::sentinel_for<I2> S2,
           class Pred = ranges::equal_to,
           class Proj1 = std::identity, class Proj2 = std::identity>
  requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
  constexpr bool
      operator()(I1 first1, S1 last1, I2 first2, S2 last2,
                 Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const
  {
      if constexpr (std::sized_sentinel_for<S1, I1> and std::sized_sentinel_for<S2, I2>)
          if (std::ranges::distance(first1, last1) != std::ranges::distance(first2, last2))
              return false;
      for (; first1 != last1; ++first1, (void)++first2)
          if (!std::invoke(pred, std::invoke(proj1, *first1), std::invoke(proj2, *first2)))
              return false;
      return true;
  }
  template<ranges::input_range R1, ranges::input_range R2,
           class Pred = ranges::equal_to,
           class Proj1 = std::identity, class Proj2 = std::identity>
  requires std::indirectly_comparable<ranges::iterator_t<R1>, ranges::iterator_t<R2>,
                                      Pred, Proj1, Proj2>
  constexpr bool
      operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const
  {
      return (*this)(ranges::begin(r1), ranges::end(r1),
                     ranges::begin(r2), ranges::end(r2),
                     std::ref(pred), std::ref(proj1), std::ref(proj2));
  }
};
inline constexpr equal_fn equal;

예제

다음 코드는 문자열이 회문인지 테스트하기 위해 ranges::equal 을 사용합니다.

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <ranges>
#include <string_view>
constexpr bool is_palindrome(const std::string_view s)
{
    namespace views = std::views;
    auto forward = s | views::take(s.size() / 2);
    auto backward = s | views::reverse | views::take(s.size() / 2);
    return std::ranges::equal(forward, backward);
}
void test(const std::string_view s)
{
    std::cout << std::quoted(s) << " is "
              << (is_palindrome(s) ? "" : "not ")
              << "a palindrome\n";
}
int main()
{
    test("radar");
    test("hello");
    static_assert(is_palindrome("ABBA") and not is_palindrome("AC/DC"));
}

출력:

"radar" is a palindrome
"hello" is not a palindrome

참고 항목

특정 기준을 만족하는 첫 번째 요소를 찾음
(알고리즘 함수 객체)
한 범위가 다른 범위보다 사전순으로 작으면 true 를 반환
(알고리즘 함수 객체)
두 범위가 처음으로 달라지는 위치를 찾음
(알고리즘 함수 객체)
요소 범위의 첫 번째 발생을 검색
(알고리즘 함수 객체)
특정 키와 일치하는 요소들의 범위를 반환
(알고리즘 함수 객체)
x == y 를 구현하는 함수 객체
(클래스 템플릿)
두 요소 집합이 동일한지 결정
(함수 템플릿)