Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::partial_sort

      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::random_access_iterator I,std::sentinel_for<I> S,

               class Comp=ranges::less,class Proj=std::identity>
      requiresstd::sortable<I, Comp, Proj>
      constexpr I

          partial_sort( I first, I middle, S last, Comp comp={}, Proj proj={});
      (1)(since C++20)
      template<ranges::random_access_range R,

               class Comp=ranges::less,class Proj=std::identity>
      requiresstd::sortable<ranges::iterator_t<R>, Comp, Proj>
      constexprranges::borrowed_iterator_t<R>
          partial_sort( R&& r,ranges::iterator_t<R> middle, Comp comp={},

                        Proj proj={});
      (2)(since C++20)
      1) Rearranges elements such that the range[firstmiddle) contains the sortedmiddle- first smallest elements in the range[firstlast).
      The order of equal elements isnot guaranteed to be preserved. The order of the remaining elements in the range[middlelast) isunspecified.
      The elements are compared using the given binary comparison functioncomp and projected usingproj function object.
      2) Same as(1), but usesr as the 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

      first, last - the iterator-sentinel pair defining therange of elements to rearrange
      r - the range of elements to rearrange
      middle - the range[firstmiddle) will be sorted
      comp - comparator to apply to the projected elements
      proj - projection to apply to the elements

      [edit]Return value

      An iterator equal tolast.

      [edit]Complexity

      \(\scriptsize \mathcal{O}(N\cdot\log{(M)})\)𝓞(N·log(M)) comparisons and twice as many projections, where\(\scriptsize N\)N isranges::distance(first, last),\(\scriptsize M\)M isranges::distance(first, middle).

      [edit]Possible implementation

      struct partial_sort_fn{template<std::random_access_iterator I,std::sentinel_for<I> S,class Comp=ranges::less,class Proj=std::identity>    requiresstd::sortable<I, Comp, Proj>constexpr I        operator()(I first, I middle, S last, Comp comp={}, Proj proj={})const{if(first== middle)returnranges::next(first, last);ranges::make_heap(first, middle, comp, proj);auto it{middle};for(; it!= last;++it){if(std::invoke(comp,std::invoke(proj,*it),std::invoke(proj,*first))){ranges::pop_heap(first, middle, comp, proj);ranges::iter_swap(middle-1, it);ranges::push_heap(first, middle, comp, proj);}}ranges::sort_heap(first, middle, comp, proj);return it;} template<ranges::random_access_range R,class Comp=ranges::less,class Proj=std::identity>    requiresstd::sortable<ranges::iterator_t<R>, Comp, Proj>constexprranges::borrowed_iterator_t<R>        operator()(R&& r,ranges::iterator_t<R> middle, Comp comp={}, Proj proj={})const{return(*this)(ranges::begin(r), std::move(middle),ranges::end(r),                       std::move(comp), std::move(proj));}}; inlineconstexpr partial_sort_fn partial_sort{};

      [edit]Example

      Run this code
      #include <algorithm>#include <functional>#include <iostream>#include <string>#include <vector> void print(constauto& v){for(constchar e: v)std::cout<< e<<' ';std::cout<<'\n';} void underscore(int n){while(n-->0)std::cout<<"^ ";std::cout<<'\n';} int main(){    static_assert('A'<'a');std::vector<char> v{'x','P','y','C','z','w','P','o'};    print(v);constint m{3};    std::ranges::partial_sort(v, v.begin()+ m);    print(v), underscore(m);     static_assert('1'<'a');std::string s{"3a1b41c5"};    print(s);    std::ranges::partial_sort(s.begin(), s.begin()+ m, s.end(),std::greater{});    print(s), underscore(m);}

      Output:

      x P y C z w P oC P P y z x w o^ ^ ^3 a 1 b 4 1 c 5c b a 1 3 1 4 5^ ^ ^

      [edit]See also

      copies and partially sorts a range of elements
      (algorithm function object)[edit]
      sorts a range into ascending order
      (algorithm function object)[edit]
      sorts a range of elements while preserving order between equal elements
      (algorithm function object)[edit]
      partially sorts the given range making sure that it is partitioned by the given element
      (algorithm function object)[edit]
      creates a max heap out of a range of elements
      (algorithm function object)[edit]
      removes the largest element from a max heap
      (algorithm function object)[edit]
      adds an element to a max heap
      (algorithm function object)[edit]
      turns a max heap into a range of elements sorted in ascending order
      (algorithm function object)[edit]
      sorts the first N elements of a range
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/ranges/partial_sort&oldid=180549"

      [8]ページ先頭

      ©2009-2025 Movatter.jp