Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |algorithm‎ |ranges
       
       
      Algorithm library
      Constrained algorithms and algorithms on ranges(C++20)
      Constrained algorithms, e.g.ranges::copy,ranges::sort, ...
      Execution policies(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
      (C++11)
      (C++17)
      Lexicographical comparison operations
      Permutation operations
      C library
      Numeric operations
      Operations on uninitialized memory
       
      Constrained algorithms
      All names in this menu belong to namespacestd::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
       
      Defined in header<algorithm>
      Call signature
      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>
      requiresstd::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)(since C++20)
      template<ranges::input_range R1,ranges::input_range R2,

               class Pred=ranges::equal_to,
               class Proj1=std::identity,class Proj2=std::identity>
      requiresstd::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)(since C++20)
      Helper types
      template<class I1,class I2>
      using mismatch_result=ranges::in_in_result<I1, I2>;
      (3)(since C++20)

      Returns the first mismatching pair of projected elements from two ranges: one defined by[first1last1) orr1 and another defined by[first2last2) orr2.

      1) Elements are compared using the given binary predicatep.
      2) Same as(1), but usesr as the source range, as if usingranges::begin(r) asfirst andranges::end(r) aslast.

      The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:

      Contents

      [edit]Parameters

      first1, last1 - the iterator-sentinel pair defining the firstrange of elements to compare
      r1 - the first range of the elements to compare
      first2, last2 - the iterator-sentinel pair defining the secondrange of elements to compare
      r2 - the second range of the elements to compare
      pred - predicate to apply to the projected elements
      proj1 - projection to apply to the first range of elements
      proj2 - projection to apply to the second range of elements

      [edit]Return value

      ranges::mismatch_result with iterators to the first two non-equal elements.

      If no mismatches are found when the comparison reacheslast1 orlast2, whichever happens first, the object holds the end iterator and the corresponding iterator from the other range.

      [edit]Complexity

      At moststd::min(last1- first1, last2- first2) applications of the predicate and corresponding projections.

      [edit]Possible implementation

      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>    requiresstd::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(notstd::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>    requiresstd::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));}}; inlineconstexpr mismatch_fn mismatch;

      [edit]Example

      This program determines the longest substring that is simultaneously found at the very beginning and at the very end of the given string, in reverse order (possibly overlapping).

      Run this code
      #include <algorithm>#include <iostream>#include <ranges>#include <string_view> [[nodiscard]]constexprstd::string_view mirror_ends(conststd::string_view in){constauto 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'; usingnamespace std::literals::string_view_literals;     static_assert("123"sv== mirror_ends("123!@#321"));    static_assert("radar"sv== mirror_ends("radar"));}

      Output:

      abaABBAlevel

      [edit]See also

      determines if two sets of elements are the same
      (algorithm function object)[edit]
      finds the first element satisfying specific criteria
      (algorithm function object)[edit]
      returnstrue if one range is lexicographically less than another
      (algorithm function object)[edit]
      searches for the first occurrence of a range of elements
      (algorithm function object)[edit]
      finds the first position where two ranges differ
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/ranges/mismatch&oldid=180462"

      [8]ページ先頭

      ©2009-2025 Movatter.jp