std::map<Key,T,Compare,Allocator>:: map
| (1) | ||
|
map
(
)
;
|
(bis C++11) | |
|
map
(
)
:
map
(
Compare
(
)
)
{
}
|
(seit C++11)
(constexpr seit C++26) |
|
|
explicit
map
(
const
Compare
&
comp,
const Allocator & alloc = Allocator ( ) ) ; |
(2) | (constexpr seit C++26) |
|
explicit
map
(
const
Allocator
&
alloc
)
;
|
(3) |
(seit C++11)
(constexpr seit C++26) |
|
template
<
class
InputIt
>
map
(
InputIt first, InputIt last,
|
(4) | (constexpr seit C++26) |
|
template
<
class
InputIt
>
map
(
InputIt first, InputIt last,
|
(5) |
(seit C++14)
(constexpr seit C++26) |
|
map
(
const
map
&
other
)
;
|
(6) | (constexpr seit C++26) |
|
map
(
const
map
&
other,
const
Allocator
&
alloc
)
;
|
(7) |
(seit C++11)
(constexpr seit C++26) |
|
map
(
map
&&
other
)
;
|
(8) |
(seit C++11)
(constexpr seit C++26) |
|
map
(
map
&&
other,
const
Allocator
&
alloc
)
;
|
(9) |
(seit C++11)
(constexpr seit C++26) |
|
map
(
std::
initializer_list
<
value_type
>
init,
const
Compare
&
comp
=
Compare
(
)
,
|
(10) |
(seit C++11)
(constexpr seit C++26) |
|
map
(
std::
initializer_list
<
value_type
>
init,
const
Allocator
&
alloc
)
|
(11) |
(seit C++14)
(constexpr seit C++26) |
|
template
<
container-compatible-range
<
value_type
>
R
>
map
(
std::
from_range_t
, R
&&
rg,
|
(12) |
(seit C++23)
(constexpr seit C++26) |
|
template
<
container-compatible-range
<
value_type
>
R
>
map
(
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
containerkompatibler 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 geprüft.
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
nicht abgeleitete Kontexte
platziert.
| Feature-Test Makro | Wert | Std | Feature |
|---|---|---|---|
__cpp_lib_containers_ranges
|
202202L
|
(C++23) | Ranges-bewusste Konstruktion und Einfügung; Überladungen ( 12,13 ) |
Beispiel
#include <iomanip> #include <iostream> #include <map> #include <string> template<typename Key, typename Value, typename Cmp> std::ostream& operator<<(std::ostream& os, const std::map<Key, Value, Cmp>& map) { os << "{ "; for (auto comma{map.size()}; const auto& p : map) os << '\'' << p.first << "' ist " << p.second << (--comma ? ", " : " "); return os << "}\n"; } struct Point { double x, y; friend std::ostream& operator<<(std::ostream& os, Point pt) { return os << '(' << pt.x << ", " << pt.y << ')'; } }; struct PointCmp { bool operator()(const Point& lhs, const Point& rhs) const { return lhs.x < rhs.x; // NB: y wird absichtlich ignoriert } }; int main() { // (1) Standardkonstruktor std::map<std::string, int> map1; map1["etwas"] = 69; map1["anything"] = 199; map1["dieses Ding"] = 50; std::cout << "map1 = " << map1; // (4) Bereichskonstruktor std::map<std::string, int> iter(map1.find("anything"), map1.end()); std::cout << "\niter = " << iter; std::cout << "map1 = " << map1; // (6) Kopierkonstruktor std::map<std::string, int> copied(map1); std::cout << "\ncopied = " << copied; std::cout << "map1 = " << map1; // (8) Move-Konstruktor std::map<std::string, int> moved{std::move(map1)}; std::cout << "\nmoved = " << moved; std::cout << "map1 = " << map1; // (10) Initialisierungslisten-Konstruktor const std::map<std::string, int> init { {"this", 100}, {"kann", 100}, {"be", 100}, {"const", 100} }; std::cout << "\ninit = " << init; std::cout << "\nCustom Key Klasse Option 1:\n"; // Verwende eine Vergleichsstruktur std::map<Point, double, PointCmp> mag = { {{5, -12}, 13}, {{3, 4}, 5}, {{-8, -15}, 17} }; std::cout << "mag = " << mag << '\n'; std::cout << "Benutzerdefinierte Key-Klasse Option 2:\n"; // Verwende ein Vergleichs-Lambda // Diese Lambda-Funktion sortiert Punkte entsprechend ihrer Beträge, wobei // diese Größen werden aus der lokalen Variable mag entnommen. auto cmpLambda = [&mag](const Point& lhs, const Point& rhs) { return mag[lhs] < mag[rhs]; }; // Sie könnten auch ein Lambda verwenden, das nicht von lokalen Variablen abhängt, wie folgt: // auto cmpLambda = [](const Point& lhs, const Point& rhs){ return lhs.y < rhs.y; }; std::map<Point, double, decltype(cmpLambda)> magy(cmpLambda); // Verschiedene Möglichkeiten zum Einfügen von Elementen: magy.insert(std::pair<Point, double>({5, -12}, 13)); magy.insert({{3, 4}, 5}); magy.insert({Point{-8.0, -15.0}, 17}); std::cout << "magy = " << magy << '\n'; std::cout << "Konstruktion aus einem Bereich:\n"; using PS = std::pair<const std::string, int>; const auto rg = {PS{"eins", 1}, {"eins", 101}, {"zwei", 2}, {"drei", 3}}; #if __cpp_lib_containers_ranges std::map<std::string, int> nums(std::from_range, rg); // Überladung (12) #else std::map<std::string, int> nums(rg.begin(), rg.end()); // Ausweichen auf (4) #endif std::cout << "nums = " << nums << '\n'; }
Ausgabe:
map1 = { 'anything' ist 199, 'something' ist 69, 'that thing' ist 50 }
iter = { 'anything' ist 199, 'something' ist 69, 'that thing' ist 50 }
map1 = { 'anything' ist 199, 'something' ist 69, 'that thing' ist 50 }
copied = { 'anything' ist 199, 'something' ist 69, 'that thing' ist 50 }
map1 = { 'anything' ist 199, 'something' ist 69, 'that thing' ist 50 }
moved = { 'anything' ist 199, 'something' ist 69, 'that thing' ist 50 }
map1 = { }
init = { 'be' ist 100, 'can' ist 100, 'const' ist 100, 'this' ist 100 }
Benutzerdefinierte Key-Klasse Option 1:
mag = { '(-8, -15)' ist 17, '(3, 4)' ist 5, '(5, -12)' ist 13 }
Benutzerdefinierte Key-Klasse Option 2:
magy = { '(3, 4)' ist 5, '(5, -12)' ist 13, '(-8, -15)' ist 17 }
Konstruktion aus einem Bereich:
nums = { 'one' ist 1, 'three' ist 3, 'two' ist 2 }
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
Key
und
T
als
CopyInsertable
in
*
this
|
nicht erforderlich |
| LWG 2193 | C++11 | der Standardkonstruktor war explizit | nicht-explizit gemacht |
Siehe auch
|
weist dem Container Werte zu
(öffentliche Elementfunktion) |