mismatch
mismatch (두 범위가 달라지는 첫 지점)
두 범위를 비교해 서로 달라지는 첫 번째 위치 쌍을 찾는 알고리즘이에요. <algorithm> 헤더에 있어요.
출처: cppreference
본문
mismatch는 [first1, last1)과 다른 범위를 앞에서부터 비교해, 처음으로 원소가 달라지는 위치를 반복자 쌍으로 반환해요.
template< class InputIt1, class InputIt2 >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1, InputIt2 first2 ); // (1)
비교기 버전과 끝 지정 버전도 있어요.
template< class InputIt1, class InputIt2, class BinaryPred >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1, InputIt2 first2,
BinaryPred p ); // (2)
- 반환 값:
{first1의 반복자, first2의 반복자}쌍. 첫 범위가 모두 일치하면{last1, first2 + (last1 - first1)}. - 첫 범위 쪽이 먼저 끝나면
{last1, ...}, 이런 지점으로 "어디까지 같고 어디서부터 다른지"를 알 수 있어요.
std::string a = "hello world";
std::string b = "hello zoo";
auto [it1, it2] = std::mismatch(a.begin(), a.end(), b.begin());
// 'w'와 'z'를 가리킴
두 시퀀스가 "어느 위치까지 같은지"를 찾을 때 써요. equal이 전체 일치 여부만 알려주는 반면, mismatch는 불일치 지점을 알려줘요.