Namensräume
Varianten
Aktionen

std::ranges::partial_sort_copy, std::ranges::partial_sort_copy_result

Von cppreference.com
< cpp‎ | algorithm‎ | ranges
 
 
Algorithmenbibliothek
Beschränkte Algorithmen und Algorithmen für Bereiche (C++20)
Beschränkte Algorithmen, z.B. ranges::copy, ranges::sort, ...
Ausführungsrichtlinien (C++17)
Nicht-modifizierende Sequenzoperationen
Stapeloperationen
(C++17)
Suchoperationen
(C++11)                (C++11)(C++11)

Modifizierende Sequenzoperationen
Kopieroperationen
(C++11)
(C++11)
Tauschoperationen
Transformationsoperationen
Generierungsoperationen
Entfernungsoperationen
Ordnungsändernde Operationen
(bis C++17)(C++11)
(C++20)(C++20)
Stichprobenoperationen
(C++17)

Sortier- und verwandte Operationen
Partitionierungsoperationen
Sortieroperationen
Binäre Suchoperationen
(auf partitionierten Bereichen)
Mengenoperationen (auf sortierten Bereichen)
Zusammenführungsoperationen (auf sortierten Bereichen)
Heapoperationen
Minimum/Maximum-Operationen
(C++11)
(C++17)
Lexikographische Vergleichsoperationen
Permutationsoperationen
C-Bibliothek
Numerische Operationen
Operationen auf uninitialisiertem Speicher
 
Eingeschränkte Algorithmen
Alle Namen in diesem Menü gehören zum Namespace std::ranges
Nicht-modifizierende Sequenzoperationen
Modifizierende Sequenzoperationen
Partitionierungsoperationen
Sortieroperationen
       
partial_sort_copy

Binäre Suchoperationen (auf sortierten Bereichen)
       
       
Mengenoperationen (auf sortierten Bereichen)
Heapoperationen
Minimum/Maximum-Operationen
       
       
Permutationsoperationen
Faltoperationen
Operationen auf uninitialisiertem Speicher
Rückgabetypen
 
Definiert in Header <algorithm>
Aufruf-Signatur
template< std::input_iterator I1, std::sentinel_for<I1> S1,

          std::random_access_iterator I2, std::sentinel_for<I2> S2,
          class Comp = ranges::less, class Proj1 = std::identity,
          class Proj2 = std::identity >
requires std::indirectly_copyable<I1, I2> &&
         std::sortable<I2, Comp, Proj2> &&
         std::indirect_strict_weak_order<Comp, std::projected<I1, Proj1>,
             std::projected<I2, Proj2>>
constexpr partial_sort_copy_result<I1, I2>
    partial_sort_copy( I1 first, S1 last, I2 result_first, S2 result_last,

                       Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
(1) (seit C++20)
template< ranges::input_range R1, ranges::random_access_range R2,

          class Comp = ranges::less, class Proj1 = std::identity,
          class Proj2 = std::identity >
requires std::indirectly_copyable<ranges::iterator_t<R1>, ranges::iterator_t<R2>> &&
         std::sortable<ranges::iterator_t<R2>, Comp, Proj2> &&
         std::indirect_strict_weak_order<Comp, std::projected<ranges::iterator_t<R1>,
             Proj1>, std::projected<ranges::iterator_t<R2>, Proj2>>
constexpr partial_sort_copy_result<ranges::borrowed_iterator_t<R1>,
                                   ranges::borrowed_iterator_t<R2>>
    partial_sort_copy( R1&& r, R2&& result_r,

                       Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
(2) (seit C++20)
Hilfstypen
template< class I, class O >
using partial_sort_copy_result = ranges::in_out_result<I, O>;
(3) (seit C++20)

Kopiert die ersten N Elemente aus dem Quellbereich [firstlast), als ob er bezüglich comp und proj1 partiell sortiert wäre, in den Zielbereich [result_firstresult_first + N), wobei N = min(L₁, L₂), L₁ gleich ranges::distance(first, last) ist und L₂ gleich ranges::distance(result_first, result_last) ist.

Die Reihenfolge gleicher Elemente ist *nicht* garantiert erhalten zu bleiben.

1) Die Elemente des Quellbereichs werden mit dem Funktions-Objekt proj1 und die Zielelemente mit dem Funktions-Objekt proj2 projiziert.
2) Entspricht (1), verwendet jedoch r als Quellbereich und result_r als Zielbereich, als ob ranges::begin(r) als first, ranges::end(r) als last, ranges::begin(result_r) als result_first und ranges::end(result_r) als result_last verwendet würden.

Die auf dieser Seite beschriebenen funktionsähnlichen Entitäten sind Algorithmus-Funktionsobjekte (informell als niebloids bekannt), d.h.

Inhalt

[edit] Parameter

first, last - das Iterator-Sentinel-Paar, das den Quell-Bereich der zu kopierenden Elemente definiert
r - der Quellbereich, aus dem kopiert werden soll
result_first, result_last - das Iterator-Sentinel-Paar, das den Ziel-Bereich der Elemente definiert
result_r - der Zielbereich
comp - Vergleich, der auf die projizierten Elemente angewendet wird
proj1 - Projektion, die auf die Elemente des Quellbereichs angewendet wird
proj2 - Projektion, die auf die Elemente des Zielbereichs angewendet wird

[edit] Rückgabewert

Ein Objekt, das gleich {last, result_first + N} ist.

[edit] Komplexität

Höchstens L₁•log(N) Vergleiche und 2•L₁•log(N) Projektionen.

[edit] Mögliche Implementierung

struct partial_sort_copy_fn
{
    template<std::input_iterator I1, std::sentinel_for<I1> S1,
             std::random_access_iterator I2, std::sentinel_for<I2> S2,
             class Comp = ranges::less, class Proj1 = std::identity,
             class Proj2 = std::identity>
    requires std::indirectly_copyable<I1, I2> && std::sortable<I2, Comp, Proj2> &&
             std::indirect_strict_weak_order<Comp, std::projected<I1, Proj1>,
             std::projected<I2, Proj2>>
    constexpr ranges::partial_sort_copy_result<I1, I2>
        operator()(I1 first, S1 last, I2 result_first, S2 result_last,
                   Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        if (result_first == result_last)
            return {std::move(ranges::next(std::move(first), std::move(last))),
                    std::move(result_first)};
 
        auto out_last{result_first};
        // copy first N elements
        for (; !(first == last or out_last == result_last); ++out_last, ++first)
            *out_last = *first;
 
        // convert N copied elements into a max-heap
        ranges::make_heap(result_first, out_last, comp, proj2);
 
        // process the rest of the input range (if any), preserving the heap property
        for (; first != last; ++first)
        {
            if (std::invoke(comp, std::invoke(proj1, *first),
                                  std::invoke(proj2, *result_first)))
            {
                // pop out the biggest item and push in a newly found smaller one
                ranges::pop_heap(result_first, out_last, comp, proj2);
                *(out_last - 1) = *first;
                ranges::push_heap(result_first, out_last, comp, proj2);
            }
        }
 
        // first N elements in the output range is still
        // a heap - convert it into a sorted range
        ranges::sort_heap(result_first, out_last, comp, proj2);
 
        return {std::move(first), std::move(out_last)};
    }
 
    template<ranges::input_range R1, ranges::random_access_range R2,
             class Comp = ranges::less, class Proj1 = std::identity,
             class Proj2 = std::identity>
    requires std::indirectly_copyable<ranges::iterator_t<R1>, ranges::iterator_t<R2>> &&
             std::sortable<ranges::iterator_t<R2>, Comp, Proj2> &&
             std::indirect_strict_weak_order<Comp, std::projected<ranges::iterator_t<R1>,
             Proj1>, std::projected<ranges::iterator_t<R2>, Proj2>>
    constexpr ranges::partial_sort_copy_result<ranges::borrowed_iterator_t<R1>,
              ranges::borrowed_iterator_t<R2>>
        operator()(R1&& r, R2&& result_r, Comp comp = {},
                   Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r),
                       ranges::begin(result_r), ranges::end(result_r),
                       std::move(comp), std::move(proj1), std::move(proj2));
    }
};
 
inline constexpr partial_sort_copy_fn partial_sort_copy {};

[edit] Beispiel

#include <algorithm>
#include <forward_list>
#include <functional>
#include <iostream>
#include <ranges>
#include <string_view>
#include <vector>
 
void print(std::string_view rem, std::ranges::input_range auto const& v)
{
    for (std::cout << rem; const auto& e : v)
        std::cout << e << ' ';
    std::cout << '\n';
}
 
int main()
{
    const std::forward_list source{4, 2, 5, 1, 3};
 
    print("Write to the smaller vector in ascending order: ", "");
 
    std::vector dest1{10, 11, 12};
    print("const source list: ", source);
    print("destination range: ", dest1);
    std::ranges::partial_sort_copy(source, dest1);
    print("partial_sort_copy: ", dest1);
 
    print("Write to the larger vector in descending order:", "");
 
    std::vector dest2{10, 11, 12, 13, 14, 15, 16};
    print("const source list: ", source);
    print("destination range: ", dest2);
    std::ranges::partial_sort_copy(source, dest2, std::greater{});
    print("partial_sort_copy: ", dest2);
}

Ausgabe

Write to the smaller vector in ascending order:
const source list: 4 2 5 1 3
destination range: 10 11 12
partial_sort_copy: 1 2 3
Write to the larger vector in descending order:
const source list: 4 2 5 1 3
destination range: 10 11 12 13 14 15 16
partial_sort_copy: 5 4 3 2 1 15 16

[edit] Siehe auch

Sortiert die ersten N Elemente eines Bereichs
(Algorithmus-Funktionsobjekt)[edit]
Sortiert einen Bereich aufsteigend
(Algorithmus-Funktionsobjekt)[edit]
Sortiert einen Bereich von Elementen und behält dabei die Reihenfolge zwischen gleichen Elementen bei
(Algorithmus-Funktionsobjekt)[edit]
Verwandelt einen Max-Heap in einen aufsteigend sortierten Bereich von Elementen
(Algorithmus-Funktionsobjekt)[edit]
Erstellt aus einem Bereich von Elementen einen Max-Heap
(Algorithmus-Funktionsobjekt)[edit]
Fügt ein Element zu einem Max-Heap hinzu
(Algorithmus-Funktionsobjekt)[edit]
Entfernt das größte Element aus einem Max-Heap
(Algorithmus-Funktionsobjekt)[edit]
Kopiert und sortiert einen Bereich von Elementen teilweise
(Funktionstemplate) [edit]