std::set<Key,Compare,Allocator>:: set
| (1) | ||
|
set
(
)
;
|
(bis C++11) | |
|
set
(
)
:
set
(
Compare
(
)
)
{
}
|
(seit C++11)
(constexpr seit C++26) |
|
|
explicit
set
(
const
Compare
&
comp,
const Allocator & alloc = Allocator ( ) ) ; |
(2) | (constexpr seit C++26) |
|
explicit
set
(
const
Allocator
&
alloc
)
;
|
(3) |
(seit C++11)
(constexpr seit C++26) |
|
template
<
class
InputIt
>
set
(
InputIt first, InputIt last,
|
(4) | (constexpr seit C++26) |
|
template
<
class
InputIt
>
set
(
InputIt first, InputIt last,
|
(5) |
(seit C++14)
(constexpr seit C++26) |
|
set
(
const
set
&
other
)
;
|
(6) | (constexpr seit C++26) |
|
set
(
const
set
&
other,
const
Allocator
&
alloc
)
;
|
(7) |
(seit C++11)
(constexpr seit C++26) |
|
set
(
set
&&
other
)
;
|
(8) |
(seit C++11)
(constexpr seit C++26) |
|
set
(
set
&&
other,
const
Allocator
&
alloc
)
;
|
(9) |
(seit C++11)
(constexpr seit C++26) |
|
set
(
std::
initializer_list
<
value_type
>
init,
const
Compare
&
comp
=
Compare
(
)
,
|
(10) |
(seit C++11)
(constexpr seit C++26) |
|
set
(
std::
initializer_list
<
value_type
>
init,
const
Allocator
&
alloc
)
|
(11) |
(seit C++14)
(constexpr seit C++26) |
|
template
<
container-compatible-range
<
value_type
>
R
>
set
(
std::
from_range_t
, R
&&
rg,
|
(12) |
(seit C++23)
(constexpr seit C++26) |
|
template
<
container-compatible-range
<
value_type
>
R
>
set
(
std::
from_range_t
, R
&&
rg,
|
(13) |
(seit C++23)
(constexpr seit C++26) |
Konstruiert einen neuen Container aus verschiedenen Datenquellen und optional unter Verwendung eines benutzerdefinierten Allokators alloc oder Vergleichsfunktionsobjekts comp .
[
first
,
last
)
.
|
Falls
alloc
nicht bereitgestellt wird, wird der Allokator durch Aufruf von
std::
allocator_traits
<
allocator_type
>
::
|
(since C++11) |
|
Während der
Klassentemplate-Argumentableitung
trägt nur das erste Argument zur Ableitung des
|
(since C++23) |
|
Während der
Klassentemplate-Argumentableitung
trägt nur das erste Argument zur Ableitung des
|
(since C++23) |
Inhaltsverzeichnis |
Parameter
| alloc | - | Allokator, der für alle Speicherallokationen dieses Containers verwendet wird |
| comp | - | Vergleichsfunktionsobjekt, das für alle Vergleiche von Schlüsseln verwendet wird |
| first, last | - | das Iteratorpaar, das die Quelle des Bereichs der zu kopierenden Elemente definiert |
| other | - | ein weiterer Container, der als Quelle zur Initialisierung der Elemente des Containers verwendet wird |
| init | - | Initialisierungsliste zur Initialisierung der Elemente des Containers |
| rg | - |
ein
container-kompatibler Bereich
, also ein
input_range
, dessen Elemente in
value_type
konvertierbar sind
|
| Typanforderungen | ||
-
InputIt
muss die Anforderungen von
LegacyInputIterator
erfüllen.
|
||
-
Compare
muss die Anforderungen von
Compare
erfüllen.
|
||
-
Allocator
muss die Anforderungen von
Allocator
erfüllen.
|
||
Komplexität
[
first
,
last
)
bereits nach
value_comp
(
)
sortiert ist.
Ausnahmen
Aufrufe von
Allocator::allocate
können eine Ausnahme auslösen.
Hinweise
Nach Container-Verschiebekonstruktion (Überladung ( 8,9 ) ) bleiben Referenzen, Zeiger und Iteratoren (außer dem End-Iterator) auf other gültig, verweisen jedoch auf Elemente, die sich nun in * this befinden. Der aktuelle Standard gibt diese Garantie durch die pauschale Aussage in [container.reqmts]/67 , und eine direktere Garantie wird durch LWG issue 2321 in Betracht gezogen.
Wenn mehrere Elemente im Bereich Schlüssel haben, die äquivalent verglichen werden, ist nicht spezifiziert, welches Element eingefügt wird (ausstehend LWG2844 ).
Obwohl es erst ab C++23 formal erforderlich ist, haben einige Implementierungen den Template-Parameter
Allocator
bereits in früheren Modi in
non-deduced contexts
platziert.
| Feature-Test Makro | Wert | Std | Feature |
|---|---|---|---|
__cpp_lib_containers_ranges
|
202202L
|
(C++23) | Ranges-basierte Konstruktion und Einfügung; Überladungen ( 12,13 ) |
Beispiel
#include <cmath> #include <iostream> #include <set> #include <string> struct Point { double x, y; }; struct PointCmp { bool operator()(const Point& lhs, const Point& rhs) const { return std::hypot(lhs.x, lhs.y) < std::hypot(rhs.x, rhs.y); } }; std::ostream& operator<<(std::ostream& os, Point pt) { return os << '(' << pt.x << ',' << pt.x << ')'; } void println(auto rem, const auto& seq) { std::cout << rem << '{'; for (auto n{seq.size()}; const auto& elm : seq) std::cout << elm << (--n ? ", " : ""); std::cout << "}\n"; } int main() { // (1) Standardkonstruktor std::set<std::string> a; a.insert("horse"); a.insert("cat"); a.insert("dog"); println("1) a: ", a); // (4) Bereichskonstruktor std::set<std::string> b(a.find("dog"), a.end()); println("2) b: ", b); // (6) Kopierkonstruktor std::set<std::string> c(a); c.insert("another horse"); println("3) c: ", c); // (8) Move-Konstruktor std::set<std::string> d(std::move(a)); println("4) d: ", d); println("5) a: ", a); // (10) Initialisierungslisten-Konstruktor std::set<std::string> e{"one", "two", "three", "five", "eight"}; println("6) e: ", e); // Benutzerdefinierter Vergleich std::set<Point, PointCmp> f = {{2, 5}, {3, 4}, {1, 1}}; f.insert({1, -1}); // Dies schlägt fehl, da die Magnitude von (1,-1) gleich (1,1) ist println("7) f: ", f); // (12) Bereichskonstruktor const auto w = {"Eurybia", "Theia", "Rhea", "Aura", "Mnemosyne", "Mnemosyne"}; #if __cpp_lib_containers_ranges std::set<std::string> g(std::from_range, w); // Überladung (12) #else std::set<std::string> g(w.begin(), w.end()); // Fallback auf (4) #endif println("8) g: ", g); }
Mögliche Ausgabe:
1) a: {Katze, Hund, Pferd}
2) b: {Hund, Pferd}
3) c: {another horse, Katze, Hund, Pferd}
4) d: {Katze, Hund, Pferd}
5) a: {}
6) e: {acht, fünf, eins, drei, zwei}
7) f: {(1,1), (3,3), (2,2)}
8) g: {Aura, Eurybia, Mnemosyne, Rhea, Theia}
Fehlerberichte
Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.
| DR | Angewendet auf | Verhalten wie veröffentlicht | Korrigiertes Verhalten |
|---|---|---|---|
| LWG 2076 | C++11 |
Überladung
(
4
)
bedingt erforderte, dass
Key
CopyInsertable
in
*
this
ist
|
nicht erforderlich |
| LWG 2193 | C++11 | der Standardkonstruktor war explicit | als non-explicit festgelegt |
Siehe auch
|
weist dem Container Werte zu
(öffentliche Elementfunktion) |