map::operator
지도 키 값이 지정 된 요소를 삽입합니다.
Type& operator[](
const Key& _Key
);
Type& operator0-(
Key&& _Key
);
매개 변수
Parameter |
설명 |
_Key |
삽입할 요소의 키 값입니다. |
반환 값
참조 삽입 된 요소의 데이터 값입니다.
설명
다음 인수 키 값이 없는 경우 해당 데이터 형식의 기본값은 함께 삽입 됩니다.
operator[]지도에 요소를 삽입 하려면 사용할 수 있습니다 m 사용 하 여 m[_Key] = DataValue; 위치 DataValue 값의 mapped_type 요소의 키 값이 _Key.
사용 하는 경우 operator[] 요소를 삽입 하려면 참조 삽입 기존 요소는 변경 되었거나 새로 만드는 여부 나타내지 않습니다.멤버 함수 찾기 및 삽입 지정 된 키를 가진 요소에 삽입 하기 전에 제공 되는지 확인 하려면 사용할 수 있습니다.
예제
// 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);
}
요구 사항
헤더: <map>
네임 스페이스: std