Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::equal_range

      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)
             
             
      equal_range
      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
      (1)
      template<std::forward_iterator I,std::sentinel_for<I> S,

               class T,class Proj=std::identity,
               std::indirect_strict_weak_order
                   <const T*, std::projected<I, Proj>> Comp=ranges::less>
      constexprranges::subrange<I> equal_range( I first, S last,const T& value,

                                                 Comp comp={}, Proj proj={});
      (since C++20)
      (until C++26)
      template<std::forward_iterator I,std::sentinel_for<I> S,

               class Proj=std::identity,
               class T= std::projected_value_t<I, Proj>,
               std::indirect_strict_weak_order
                   <const T*, std::projected<I, Proj>> Comp=ranges::less>
      constexprranges::subrange<I> equal_range( I first, S last,const T& value,

                                                 Comp comp={}, Proj proj={});
      (since C++26)
      (2)
      template<ranges::forward_range R,

               class T,class Proj=std::identity,
               std::indirect_strict_weak_order
                   <const T*, std::projected<ranges::iterator_t<R>,
                                              Proj>> Comp=ranges::less>
      constexprranges::borrowed_subrange_t<R>

          equal_range( R&& r,const T& value, Comp comp={}, Proj proj={});
      (since C++20)
      (until C++26)
      template<ranges::forward_range R,

               class Proj=std::identity,
               class T= std::projected_value_t<ranges::iterator_t<R>, Proj>,
               std::indirect_strict_weak_order
                   <const T*, std::projected<ranges::iterator_t<R>,
                                              Proj>> Comp=ranges::less>
      constexprranges::borrowed_subrange_t<R>

          equal_range( R&& r,const T& value, Comp comp={}, Proj proj={});
      (since C++26)
      1) Returns a view containing all elements equivalent tovalue in the range[firstlast).

      The range[firstlast) must be at least partially ordered with respect tovalue, i.e. it must satisfy all of the following requirements:

      • partitioned with respect toelement< value orcomp(element, value) (that is, all elements for which the expression istrue precedes all elements for which the expression isfalse).
      • partitioned with respect to!(value< element) or!comp(value, element).
      • for all elements, ifelement< value orcomp(element, value) istrue then!(value< element) or!comp(value, element) is alsotrue.

      A fully-sorted range meets these criteria.

      The returned view is constructed from two iterators, one pointing to the first element that isnot less thanvalue and another pointing to the first elementgreater thanvalue. The first iterator may be alternatively obtained withstd::ranges::lower_bound(), the second - withstd::ranges::upper_bound().

      2) Same as(1), but usesr as the source range, as if using the rangeranges::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

      first, last - the iterator-sentinel pair defining therange of elements to examine
      r - the range of the elements to examine
      value - value to compare the elements to
      comp - if the first argument isless than (i.e. is ordered before) the second
      proj - projection to apply to the elements

      [edit]Return value

      std::ranges::subrange containing a pair of iterators defining the wanted range, the first pointing to the first element that isnot less thanvalue and the second pointing to the first elementgreater thanvalue.

      If there are no elementsnot less thanvalue, the last iterator (iterator that is equal tolast orranges::end(r)) is returned as the first element. Similarly if there are no elementsgreater thanvalue, the last iterator is returned as the second element.

      [edit]Complexity

      The number of comparisons performed is logarithmic in the distance betweenfirst andlast (at most2 * log2(last - first) + O(1) comparisons). However, for an iterator that does not modelrandom_access_iterator, the number of iterator increments is linear.

      [edit]Possible implementation

      struct equal_range_fn{template<std::forward_iterator I,std::sentinel_for<I> S,class Proj=std::identity,class T= std::projected_value_t<I, Proj>,std::indirect_strict_weak_order<const T*, std::projected<I, Proj>> Comp=ranges::less>constexprranges::subrange<I>        operator()(I first, S last,const T& value, Comp comp={}, Proj proj={})const{returnranges::subrange(ranges::lower_bound(first, last, value,std::ref(comp),std::ref(proj)),ranges::upper_bound(first, last, value,std::ref(comp),std::ref(proj)));} template<ranges::forward_range R,class Proj=std::identity,class T= std::projected_value_t<ranges::iterator_t<R>, Proj>,std::indirect_strict_weak_order<const T*, std::projected<ranges::iterator_t<R>,                                           Proj>> Comp=ranges::less>constexprranges::borrowed_subrange_t<R>        operator()(R&& r,const T& value, Comp comp={}, Proj proj={})const{return(*this)(ranges::begin(r),ranges::end(r), value,std::ref(comp),std::ref(proj));}}; inlineconstexpr equal_range_fn equal_range;

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_algorithm_default_value_type202403(C++26)List-initialization for algorithms(1,2)

      [edit]Example

      Run this code
      #include <algorithm>#include <compare>#include <complex>#include <iostream>#include <vector> struct S{int number{};char name{};// note: name is ignored by these comparison operatorsfriendbool operator==(const S s1,const S s2){return s1.number== s2.number;}friendauto operator<=>(const S s1,const S s2){return s1.number<=> s2.number;}friendstd::ostream& operator<<(std::ostream& os, S o){return os<<'{'<< o.number<<", '"<< o.name<<"'}";}}; void println(auto rem,constauto& v){for(std::cout<< rem;constauto& e: v)std::cout<< e<<' ';std::cout<<'\n';} int main(){// note: not ordered, only partitioned w.r.t. S defined belowstd::vector<S> vec{{1,'A'},{2,'B'},{2,'C'},{2,'D'},{4,'D'},{4,'G'},{3,'F'}}; const S value{2,'?'}; namespace ranges= std::ranges; auto a= ranges::equal_range(vec, value);    println("1. ", a); auto b= ranges::equal_range(vec.begin(), vec.end(), value);    println("2. ", b); auto c= ranges::equal_range(vec,'D',ranges::less{},&S::name);    println("3. ", c); auto d= ranges::equal_range(vec.begin(), vec.end(),'D',ranges::less{},&S::name);    println("4. ", d); using CD=std::complex<double>;std::vector<CD> nums{{1,0},{2,2},{2,1},{3,0},{3,1}};auto cmpz=[](CD x, CD y){return x.real()< y.real();};#ifdef __cpp_lib_algorithm_default_value_typeauto p3= ranges::equal_range(nums,{2,0}, cmpz);#elseauto p3= ranges::equal_range(nums, CD{2,0}, cmpz);#endif    println("5. ", p3);}

      Output:

      1. {2, 'B'} {2, 'C'} {2, 'D'}2. {2, 'B'} {2, 'C'} {2, 'D'}3. {2, 'D'} {4, 'D'}4. {2, 'D'} {4, 'D'}5. (2,2) (2,1)

      [edit]See also

      returns an iterator to the first elementnot less than the given value
      (algorithm function object)[edit]
      returns an iterator to the first elementgreater than a certain value
      (algorithm function object)[edit]
      determines if an element exists in a partially-ordered range
      (algorithm function object)[edit]
      divides a range of elements into two groups
      (algorithm function object)[edit]
      determines if two sets of elements are the same
      (algorithm function object)[edit]
      returns range of elements matching a specific key
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/ranges/equal_range&oldid=180599"

      [8]ページ先頭

      ©2009-2025 Movatter.jp