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) |
InhalteParameter
Komplexität
1-3)
Konstante.
4,5)
N·log(N)
wobei
N
gleich
std::
distance
(
first, last
)
ist, im Allgemeinen linear in
N
falls
[
first
,
last
)
bereits nach
value_comp
(
)
sortiert ist.
6,7)
Linear in der Größe von
other
.
8,9)
Konstant. Wenn
alloc
angegeben ist und
alloc
!
=
other.
get_allocator
(
)
, dann linear.
10,11)
N·log(N)
wobei
N
die
init.
size
(
)
ist, im Allgemeinen linear in
N
falls
init
bereits nach
value_comp
(
)
sortiert ist.
12,13)
N·log(N)
wobei
N
ranges::
distance
(
rg
)
entspricht, im Allgemeinen linear in
N
falls
rg
bereits durch
value_comp
(
)
sortiert ist.
Ausnahmen
Aufrufe von
AnmerkungenNach der Container-Verschiebungskonstruktion (Überladung (8,9)) bleiben Referenzen, Zeiger und Iteratoren, die ursprünglich auf Elemente in Obwohl formal erst ab C++23 erforderlich, haben einige Implementierungen den Template-Parameter
Beispiel
Diesen Code ausführen
#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}
FehlerberichteDie folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.
Siehe auch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||