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) |
InhaltParameter
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
)
ist, im Allgemeinen linear in
N
falls
rg
bereits durch
value_comp
(
)
sortiert ist.
Ausnahmen
Aufrufe von
AnmerkungenNach der Verschiebungskonstruktion eines Containers (Überladung (8,9)) bleiben Referenzen, Zeiger und Iteratoren, die ursprünglich auf Elemente in Obwohl erst ab C++23 formal erforderlich, haben einige Implementierungen den Template-Parameter
Beispiel
Diesen Code ausführen
#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 }
FehlerberichteDie folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.
Siehe auch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||