std::ranges:: partition_copy, std::ranges:: partition_copy_result
std::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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Definiert in Header
<algorithm>
|
||
|
Aufrufsignatur
|
||
|
template
<
std::
input_iterator
I,
std::
sentinel_for
<
I
>
S,
std::
weakly_incrementable
O1,
std::
weakly_incrementable
O2,
|
(1) | (seit C++20) |
|
template
<
ranges::
input_range
R,
std::
weakly_incrementable
O1,
std::
weakly_incrementable
O2,
|
(2) | (seit C++20) |
|
Hilfstypen
|
||
|
template
<
class
I,
class
O1,
class
O2
>
using partition_copy_result = ranges:: in_out_out_result < I, O1, O2 > ; |
(3) | (seit C++20) |
[
first
,
last
)
in zwei verschiedene Ausgabebereiche, abhängig vom durch das Prädikat
pred
zurückgegebenen Wert. Die Elemente, die das Prädikat
pred
nach der Projektion durch
proj
erfüllen, werden in den bei
out_true
beginnenden Bereich kopiert. Die restlichen Elemente werden in den bei
out_false
beginnenden Bereich kopiert. Das Verhalten ist undefiniert, wenn der Eingabebereich einen der Ausgabebereiche überlappt.
Die auf dieser Seite beschriebenen funktionsähnlichen Entitäten sind algorithm function objects (informell bekannt als niebloids ), das heißt:
- Explizite Template-Argumentlisten können beim Aufruf keiner von ihnen angegeben werden.
- Keiner von ihnen ist sichtbar für argument-dependent lookup .
- Wenn einer von ihnen durch normal unqualified lookup als Name links vom Funktionsaufrufoperator gefunden wird, wird argument-dependent lookup unterdrückt.
Inhaltsverzeichnis |
Parameter
| first, last | - | das Iterator-Sentinel-Paar, das den Quell- Bereich der zu kopierenden Elemente definiert |
| r | - | der Quellbereich der zu kopierenden Elemente |
| out_true | - | der Anfang des Ausgabebereichs für die Elemente, die pred erfüllen |
| out_false | - | der Anfang des Ausgabebereichs für die Elemente, die pred nicht erfüllen |
| pred | - | Prädikat, das auf die projizierten Elemente angewendet wird |
| proj | - | Projektion, die auf die Elemente angewendet wird |
Rückgabewert
{
last, o1, o2
}
, wobei
o1
und
o2
die Enden der Ausgabebereiche sind, nachdem das Kopieren abgeschlossen ist.
Komplexität
Genau ranges:: distance ( first, last ) Anwendungen des entsprechenden Prädikats comp und jeglicher Projektion proj .
Mögliche Implementierung
struct partition_copy_fn { template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O1, std::weakly_incrementable O2, class Proj = std::identity, std::indirect_unary_predicate< std::projected<I, Proj>> Pred> requires std::indirectly_copyable<I, O1> && std::indirectly_copyable<I, O2> constexpr ranges::partition_copy_result<I, O1, O2> operator()(I first, S last, O1 out_true, O2 out_false, Pred pred, Proj proj = {}) const { for (; first != last; ++first) if (!!std::invoke(pred, std::invoke(proj, *first))) *out_true = *first, ++out_true; else *out_false = *first, ++out_false; return {std::move(first), std::move(out_true), std::move(out_false)}; } template<ranges::input_range R, std::weakly_incrementable O1, std::weakly_incrementable O2, class Proj = std::identity, std::indirect_unary_predicate<std::projected<iterator_t<R>, Proj>> Pred> requires std::indirectly_copyable<ranges::iterator_t<R>, O1> && std::indirectly_copyable<ranges::iterator_t<R>, O2> constexpr ranges::partition_copy_result<ranges::borrowed_iterator_t<R>, O1, O2> operator()(R&& r, O1 out_true, O2 out_false, Pred pred, Proj proj = {}) const { return (*this)(ranges::begin(r), ranges::end(r), std::move(out_true), std::move(out_false), std::move(pred), std::move(proj)); } }; inline constexpr partition_copy_fn partition_copy {}; |
Beispiel
#include <algorithm> #include <cctype> #include <iostream> #include <iterator> #include <vector> int main() { const auto in = {'N', '3', 'U', 'M', '1', 'B', '4', 'E', '1', '5', 'R', '9'}; std::vector<int> o1(size(in)), o2(size(in)); auto pred = [](char c) { return std::isalpha(c); }; auto ret = std::ranges::partition_copy(in, o1.begin(), o2.begin(), pred); std::ostream_iterator<char> cout {std::cout, " "}; std::cout << "in = "; std::ranges::copy(in, cout); std::cout << "\no1 = "; std::copy(o1.begin(), ret.out1, cout); std::cout << "\no2 = "; std::copy(o2.begin(), ret.out2, cout); std::cout << '\n'; }
Ausgabe:
in = N 3 U M 1 B 4 E 1 5 R 9 o1 = N U M B E R o2 = 3 1 4 1 5 9
Siehe auch
|
(C++20)
|
unterteilt eine Reihe von Elementen in zwei Gruppen
(Algorithmus-Funktionsobjekt) |
|
(C++20)
|
unterteilt Elemente in zwei Gruppen unter Beibehaltung ihrer relativen Reihenfolge
(Algorithmus-Funktionsobjekt) |
|
(C++20)
(C++20)
|
kopiert eine Reihe von Elementen an einen neuen Speicherort
(Algorithmus-Funktionsobjekt) |
|
(C++20)
(C++20)
|
kopiert eine Reihe von Elementen unter Auslassung bestimmter Kriterien
(Algorithmus-Funktionsobjekt) |
|
(C++11)
|
kopiert eine Reihe und unterteilt die Elemente in zwei Gruppen
(Funktionstemplate) |