equal

equal (두 범위가 같은지 검사)

두 범위의 원소를 하나씩 비교해 서로 같은지 확인하는 알고리즘이에요. <algorithm> 헤더에 있어요.

출처: cppreference

본문

equal은 범위 [first1, last1)과 다른 범위를 비교해 모든 원소가 일치하는지 판정해요.

template< class InputIt1, class InputIt2 >
bool equal( InputIt1 first1, InputIt1 last1,
            InputIt2 first2 );                    // (1)

template< class InputIt1, class InputIt2, class BinaryPred >
bool equal( InputIt1 first1, InputIt1 last1,
            InputIt2 first2, BinaryPred p );      // (2)
    1. operator==로 비교해요.
    1. 이진 술어 p로 비교해요.

C++14부터는 두 번째 범위의 끝을 지정하는 4-인자 오버로드도 있어요.

template< class InputIt1, class InputIt2 >
bool equal( InputIt1 first1, InputIt1 last1,
            InputIt2 first2, InputIt2 last2 );    // (3)

첫 범위의 모든 원소가 두 번째 범위의 대응 원소와 같으면 true를 반환해요. 두 범위의 길이가 다르면 당연히 false예요(끝을 지정한 버전).

std::vectoroperator==로 비교되지만, 원시 배열이나 다른 반복자 컨테이너의 원소 비교에 equal을 쓰면 좋아요.

std::vector<int> a{1, 2, 3};
std::array<int, 3> b{1, 2, 3};
bool same = std::equal(a.begin(), a.end(), b.begin());   // true

4-인자 버전은 길이까지 검사하므로 더 안전해요(C++14 이후).

더 알아보기 (Learn more)

cppreference