Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::equal

      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>
      constexprbool
          equal( 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>
      constexprbool

          equal( R1&& r1, R2&& r2, Pred pred={}, Proj1 proj1={}, Proj2 proj2={});
      (2)(since C++20)
      1) Returnstrue if the projected values of the range[first1last1) are equal to the projected values of the range[first2last2), andfalse otherwise.
      2) Same as(1), but usesr as the source range, as if usingranges::begin(r) asfirst andranges::end(r) aslast.

      Two ranges are considered equal if they have the same number of elements and every pair of corresponding projected elements satisfiespred. That is,std::invoke(pred,std::invoke(proj1,*first1),std::invoke(proj2,*first2)) returnstrue for all pairs of corresponding elements in both ranges.

      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 - binary 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

      If the length of the range[first1last1) does not equal the length of the range[first2last2), returnsfalse.

      If the elements in the two ranges are equal after projection, returnstrue.

      Otherwise returnsfalse.

      [edit]Notes

      ranges::equal should not be used to compare the ranges formed by the iterators fromstd::unordered_set,std::unordered_multiset,std::unordered_map, orstd::unordered_multimap because the order in which the elements are stored in those containers may be different even if the two containers store the same elements.

      When comparing entire containers or string views for equality,operator== for the corresponding type are usually preferred.

      ranges::equal is not guaranteed to be short-circuit. E.g. if the first pair elements of both ranges do not compare equal, the rest of elements may also be compared. Non-short-circuit comparison may happen when the ranges are compared withstd::memcmp or implementation-specific vectorized algorithms.

      [edit]Complexity

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

      However, ifS1 andS2 both modelstd::sized_sentinel_for their respective iterators, andlast1- first1!= last2- first2 then no applications of the predicate are made (size mismatch is detected without looking at any elements).

      [edit]Possible implementation

      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>  requiresstd::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>constexprbool      operator()(I1 first1, S1 last1, I2 first2, S2 last2,                 Pred pred={}, Proj1 proj1={}, Proj2 proj2={})const{ifconstexpr(std::sized_sentinel_for<S1, I1> andstd::sized_sentinel_for<S2, I2>)if(std::ranges::distance(first1, last1)!= std::ranges::distance(first2, last2))returnfalse; for(; first1!= last1;++first1,(void)++first2)if(!std::invoke(pred,std::invoke(proj1,*first1),std::invoke(proj2,*first2)))returnfalse;returntrue;} 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>constexprbool      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 equal_fn equal;

      [edit]Example

      The following code usesranges::equal to test if a string is a palindrome.

      Run this code
      #include <algorithm>#include <iomanip>#include <iostream>#include <ranges>#include <string_view> constexprbool is_palindrome(conststd::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(conststd::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"));}

      Output:

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

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp