Namespaces
Variants

std::ranges:: mismatch, std::ranges:: mismatch_result

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 mismatch_result < I1, I2 >
mismatch ( 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 mismatch_result < ranges:: borrowed_iterator_t < R1 > ,
ranges:: borrowed_iterator_t < R2 >>
mismatch ( R1 && r1, R2 && r2, Pred pred = { } ,

Proj1 proj1 = { } , Proj2 proj2 = { } ) ;
(2) (C++20부터)
헬퍼 타입
template < class I1, class I2 >
using mismatch_result = ranges:: in_in_result < I1, I2 > ;
(3) (C++20부터)

두 범위에서 투영된 요소들의 첫 번째 불일치 쌍을 반환합니다: 하나는 [ first1 , last1 ) 또는 r1 로 정의되고, 다른 하나는 [ first2 , last2 ) 또는 r2 로 정의됩니다.

1) 요소들은 주어진 이항 조건자 p 를 사용하여 비교됩니다.
2) (1) 과 동일하지만, r 을 소스 범위로 사용하며, 마치 ranges:: begin ( r ) first 로, ranges:: end ( r ) last 로 사용하는 것과 같습니다.

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

목차

매개변수

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

반환값

ranges::mismatch_result 첫 번째로 일치하지 않는 두 요소에 대한 반복자.

비교가 last1 또는 last2 에 도달할 때까지 불일치가 발견되지 않으면, 객체는 끝 반복자와 다른 범위의 해당 반복자를 보유합니다.

복잡도

최대 std:: min ( last1 - first1, last2 - first2 ) 번의 술어 및 해당 프로젝션 적용.

가능한 구현

struct mismatch_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 std::mismatch_result<I1, I2>
        operator()(I1 first1, S1 last1, I2 first2, S2 last2,
                   Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        for (; first1 != last1 && first2 != last2; ++first1, (void)++first2)
            if (not std::invoke(pred, std::invoke(proj1, *first1),
                                      std::invoke(proj2, *first2)))
                break;
        return {first1, first2};
    }
    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 ranges::mismatch_result<ranges::borrowed_iterator_t<R1>,
                                      ranges::borrowed_iterator_t<R2>>
        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 mismatch_fn mismatch;

예제

이 프로그램은 주어진 문자열의 맨 처음과 맨 끝에서 동시에 발견되는 가장 긴 부분 문자열을 역순으로(겹칠 수 있음) 결정합니다.

#include <algorithm>
#include <iostream>
#include <ranges>
#include <string_view>
[[nodiscard]]
constexpr std::string_view mirror_ends(const std::string_view in)
{
    const auto end = std::ranges::mismatch(in, in | std::views::reverse).in1;
    return {in.cbegin(), end};
}
int main()
{
    std::cout << mirror_ends("abXYZba") << '\n'
              << mirror_ends("abca") << '\n'
              << mirror_ends("ABBA") << '\n'
              << mirror_ends("level") << '\n';
    using namespace std::literals::string_view_literals;
    static_assert("123"sv == mirror_ends("123!@#321"));
    static_assert("radar"sv == mirror_ends("radar"));
}

출력:

ab
a
ABBA
level

참고 항목

두 원소 집합이 동일한지 결정
(알고리즘 함수 객체)
특정 기준을 만족하는 첫 번째 원소를 찾음
(알고리즘 함수 객체)
true 를 반환 - 한 범위가 다른 범위보다 사전순으로 작은 경우
(알고리즘 함수 객체)
원소 범위의 첫 번째 발생을 검색
(알고리즘 함수 객체)
두 범위가 처음으로 달라지는 위치를 찾음
(함수 템플릿)