Namespaces
Variants

std::map<Key,T,Compare,Allocator>:: map

From cppreference.net

(Anmerkung: Der bereitgestellte HTML-Code enthält keinen übersetzbaren Text, da alle Tags und Attribute gemäß den Anweisungen unverändert bleiben sollen und die Tabellenzellen leer sind.)
(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,
const Compare & comp = Compare ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(4) (constexpr seit C++26)
template < class InputIt >

map ( InputIt first, InputIt last,
const Allocator & alloc )

: map ( first, last, Compare ( ) , alloc ) { }
(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 ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(10) (seit C++11)
(constexpr seit C++26)
map ( std:: initializer_list < value_type > init,

const Allocator & alloc )

: map ( init, Compare ( ) , alloc ) { }
(11) (seit C++14)
(constexpr seit C++26)
template < container-compatible-range < value_type > R >

map ( std:: from_range_t , R && rg,
const Compare & comp = Compare ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(12) (seit C++23)
(constexpr seit C++26)
template < container-compatible-range < value_type > R >

map ( std:: from_range_t , R && rg,
const Allocator & alloc )

: map ( std:: from_range , std:: forward < R > ( rg ) , Compare ( ) , alloc ) { }
(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 .

1-3) Konstruiert einen leeren Container.
4,5) Konstruiert den Container mit den Inhalten des Bereichs [ first , last ) .
Falls [ first , last ) kein gültiger Bereich ist, ist das Verhalten undefiniert.
6,7) Konstruiert den Container mit einer Kopie der Inhalte von other .

Falls alloc nicht bereitgestellt wird, wird der Allokator durch Aufruf von std:: allocator_traits < allocator_type > ::
select_on_container_copy_construction ( other. get_allocator ( ) )
erhalten.

(since C++11)

Während der Klassentemplate-Argumentableitung trägt nur das erste Argument zur Ableitung des Allocator -Templateparameters des Containers bei.

(since C++23)
8,9) Konstruiert den Container mit den Inhalten von other unter Verwendung von Move-Semantik. Wenn alloc nicht bereitgestellt wird, wird der Allokator durch Move-Konstruktion vom Allokator von other bezogen.

Während der Klassentemplate-Argumentableitung trägt nur das erste Argument zur Ableitung des Allocator -Templateparameters des Containers bei.

(since C++23)
10,11) Konstruiert den Container mit den Inhalten der Initialisierungsliste init .
12,13) Konstruiert den Container mit den Inhalten von rg .

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

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 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)