map::operator

Fügt ein Element in eine Zuordnung mit einem angegebenen Schlüsselwert ein.

Type& operator[](
   const Key& _Key
);
Type& operator0-(
    Key&& _Key
);

Parameter

Parameter

Description

_Key

Der Schlüsselwert des Elements, das eingefügt werden soll.

Rückgabewert

Ein Verweis auf den Datenwert des eingefügten Elements.

Hinweise

Wenn der Argumentschlüsselwert nicht gefunden wird, wird diese zusammen mit dem Standardwert des Datentyps eingefügt.

operator[] wird auf dem Ziel in eine Zuordnung m mithilfe m[_Key] = DataValue; verwendet werden, in dem DataValue der Wert mapped_type des Elements mit einem Schlüsselwert von _Key ist.

Wenn er operator[] zu dem Ziel verwendet, gibt der zurückgegebene Verweis nicht an, ob eine Einfügung ein bereits vorhandenen Element geändert oder eine neue erstellt.Die Memberfunktionen Suche und Einfügen können verwendet werden, um zu bestimmen, ob ein Element mit einem bestimmten Schlüssel bereits vor einer Einfügung vorhanden ist.

Beispiel

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

int main( )
{
   using namespace std;
   typedef pair <const int, int> cInt2Int;
   map <int, int> m1;
   map <int, int> :: iterator pIter;
   
   // Insert a data value of 10 with a key of 1
   // into a map using the operator[] member function
   m1[ 1 ] = 10;

   // Compare other ways to insert objects into a map
   m1.insert ( map <int, int> :: value_type ( 2, 20 ) );
   m1.insert ( cInt2Int ( 3, 30 ) );

   cout  << "The keys of the mapped elements are:";
   for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
      cout << " " << pIter -> first;
   cout << "." << endl;

   cout  << "The values of the mapped elements are:";
   for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
      cout << " " << pIter -> second;
   cout << "." << endl;

   // If the key already exists, operator[]
   // changes the value of the datum in the element
   m1[ 2 ] = 40;

   // operator[] will also insert the value of the data
   // type's default constructor if the value is unspecified
   m1[5];

   cout  << "The keys of the mapped elements are now:";
   for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
      cout << " " << pIter -> first;
   cout << "." << endl;

   cout  << "The values of the mapped elements are now:";
   for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
      cout << " " << pIter -> second;
   cout << "." << endl;

// insert by moving key
    map<string, int> c2;
    string str("abc");
    cout << "c2[move(str)] == " << c2[move(str)] << endl;
    cout << "c2["abc"] == " << c2["abc"] << endl;

    return (0); 
}
  
  
  
  
  

Anforderungen

Header: <map>

Namespace: std

Siehe auch

Referenz

map Class

Standardvorlagenbibliothek