Freigeben über


map::map

Erstellt eine Zuordnung, die leer oder die Kopie einer vollständigen anderen Zuordnung oder eines Teils davon ist.

map( );
explicit map(
    const Traits& Comp
);
map(
    const Traits& Comp,
    const Allocator& Al
);
map(
    const map& Right
); 
map(
    map&& Right
);
map(
    initializer_list<value_type> IList
);
map(
    initializer_list<value_type> IList,
    const Traits& Comp
);
map(
    initializer_list<value_type> IList,
    const Traits& Comp, 
    const Allocator& Allocator
);
template<class InputIterator>
    map(
        InputIterator First,
        InputIterator Last
    );
template<class InputIterator>
    map(
        InputIterator First,
        InputIterator Last,
        const Traits& Comp
    );
template<class InputIterator>
    map(
        InputIterator First,
        InputIterator Last,
        const Traits& Comp,
        const Allocator& Al
    );

Parameter

Parameter

Beschreibung

Al

Die für dieses Zuordnungsobjekt zu verwendende Speicherzuweisungsklasse, dessen Standard Allocator ist.

Comp

Die Vergleichsfunktion vom Typ const Traits, die verwendet wird, um die Elemente in der Zuordnung zu sortieren, deren Standard hash_compare ist.

Right

Die Zuordnung, deren Kopie der erstellte Satz sein soll.

First

Die Position des ersten Elements in dem zu kopierenden Elementbereich.

Last

Die Position des ersten Elements nach dem zu kopierenden Elementbereich.

IList

Das initializer_list-Element, von dem die Elemente kopiert werden sollen.

Hinweise

In allen Konstruktoren wird ein Zuweisungsobjekttyp gespeichert, mit dem der Arbeitsspeicher für die Zuordnung verwaltet wird und die später zurückgegeben werden kann, indem get_allocator aufgerufen wird. Der Zuweisungsparameter wird häufig aus den Klassendeklarationen und den Vorverarbeitungsmakros weggelassen, die zum Ersetzen alternativer Zuweisungen verwendet werden.

Alle Konstruktoren initialisieren die Zuordnung.

In allen Konstruktoren wird ein Funktionsobjekt vom Typ "Traits" gespeichert, der verwendet wird, um unter den Schlüsseln der Zuordnung eine Sortierung vorzunehmen und die später zurückgegeben werden kann, indem key_comp aufgerufen wird.

Die ersten drei Konstruktoren geben eine leere ursprüngliche Zuordnung an. Dabei gibt der Zweite den Typ der Vergleichsfunktion (Comp) an, die zum Angeben der Reihenfolge der Elemente verwendet wird, und der Dritte gibt explizit den zu verwendenden Zuweisungstyp (Al) an. Mit dem Schlüsselwort explicit werden bestimmte Arten automatischer Typumwandlung unterdrückt.

Der vierte Konstruktor gibt eine Kopie der Right-Zuordnung an.

Der fünfte Konstruktor gibt eine Kopie der Zuordnung an, indem Right verschoben wird.

Der sechste, siebte und achte Konstruktor verwendet ein initializer_list-Element, aus dem die Member kopiert werden.

Mit den nächsten drei Konstruktoren wird der [First, Last)-Bereich, einer Zuordnung kopiert, wobei sich die Explizitheit bei Angabe des Typs der Vergleichsfunktion der Klasse Traits und "Allocator" erhöht.

Beispiel

// map_map.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main()
{
    using namespace std;
    typedef pair <int, int> Int_Pair;
    map <int, int>::iterator m1_Iter, m3_Iter, m4_Iter, m5_Iter, m6_Iter, m7_Iter;
    map <int, int, less<int> >::iterator m2_Iter;

    // Create an empty map m0 of key type integer
    map <int, int> m0;

    // Create an empty map m1 with the key comparison
    // function of less than, then insert 4 elements
    map <int, int, less<int> > m1;
    m1.insert(Int_Pair(1, 10));
    m1.insert(Int_Pair(2, 20));
    m1.insert(Int_Pair(3, 30));
    m1.insert(Int_Pair(4, 40));

    // Create an empty map m2 with the key comparison
    // function of geater than, then insert 2 elements
    map <int, int, less<int> > m2;
    m2.insert(Int_Pair(1, 10));
    m2.insert(Int_Pair(2, 20));

    // Create a map m3 with the 
    // allocator of map m1
    map <int, int>::allocator_type m1_Alloc;
    m1_Alloc = m1.get_allocator();
    map <int, int> m3(less<int>(), m1_Alloc);
    m3.insert(Int_Pair(3, 30));

    // Create a copy, map m4, of map m1
    map <int, int> m4(m1);

    // Create a map m5 by copying the range m1[_First, _Last)
    map <int, int>::const_iterator m1_bcIter, m1_ecIter;
    m1_bcIter = m1.begin();
    m1_ecIter = m1.begin();
    m1_ecIter++;
    m1_ecIter++;
    map <int, int> m5(m1_bcIter, m1_ecIter);

    // Create a map m6 by copying the range m4[_First, _Last)
    // and with the allocator of map m2
    map <int, int>::allocator_type m2_Alloc;
    m2_Alloc = m2.get_allocator();
    map <int, int> m6(m4.begin(), ++m4.begin(), less<int>(), m2_Alloc);

    cout << "m1 =";
    for (auto i : m1)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    cout << "m2 =";
    for(auto i : m2)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    cout << "m3 =";
    for (auto i : m3)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    cout << "m4 =";
    for (auto i : m4)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    cout << "m5 =";
    for (auto i : m5)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    cout << "m6 =";
    for (auto i : m6)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    // Create a map m7 by moving m5
    cout << "m7 =";
    map<int, int> m7(move(m5));
    for (auto i : m7)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    // Create a map m8 by copying in an initializer_list
    map<int, int> m8{ { { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 } } };
    cout << "m8: = ";
    for (auto i : m8)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    // Create a map m9 with an initializer_list and a comparator
    map<int, int> m9({ { 5, 5 }, { 6, 6 }, { 7, 7 }, { 8, 8 } }, less<int>());
    cout << "m9: = ";
    for (auto i : m9)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    // Create a map m10 with an initializer_list, a comparator, and an allocator
    map<int, int> m10({ { 9, 9 }, { 10, 10 }, { 11, 11 }, { 12, 12 } }, less<int>(), m9.get_allocator());
    cout << "m10: = ";
    for (auto i : m10)
        cout << i.first << " " << i.second << ", ";
    cout << endl;
}

Ausgabe

m1 = 10 20 30 40
m2 = 10 20
m3 = 30
m4 = 10 20 30 40
m5 = 10 20
m6 = 10
m7 = 10 20
m8: = 1 1, 2 2, 3 3, 4 4,
m9: = 5 5, 6 6, 7 7, 8 8,
m10: = 9 9, 10 10, 11 11, 12 12,

Anforderungen

Header: <map>

Namespace: std

Siehe auch

Referenz

map-Klasse

Standardvorlagenbibliothek