map::insert
요소 또는 요소 범위에 지도 삽입합니다.
pair <iterator, bool> insert(
const value_type& _Val
);
iterator insert(
const_iterator _Where,
const value_type& _Val
);
template<class InputIterator>
void insert(
InputIterator _First,
InputIterator _Last
);
template<class ValTy>
pair<iterator, bool> insert(
ValTy&& _Val
);
template<class ValTy>
iterator insert(
const_iterator _Where,
ValTy&& _Val
);
매개 변수
Parameter |
설명 |
_Val |
지도에 지도 요소 또는 더 일반적으로 키 정렬 변환과 요소가 이미 포함 되어 있지 않으면 삽입할 요소의 값입니다. |
_Where |
올바른 삽입 지점에 대 한 검색을 시작할 위치에 대 한 힌트입니다. |
_First |
지도에서 복사할 첫 번째 요소의 위치입니다. |
_Last |
지도에서 복사 하는 마지막 요소는 위치입니다. |
반환 값
첫 번째 삽입 지도 이미 키 같은 값의 순서에 있어 반복기 요소가 반환 주소 새 요소가 삽입 된 또는 요소가 이미 배치 된 요소에 포함 된 경우 false 부울 요소가 삽입 된 경우 true를 반환 하 고 멤버 함수 쌍을 반환 합니다.
한 쌍의 반복기 구성 요소에 액세스 하려면 pr 멤버 함수에 의해 반환 된 사용 pr. 첫 번째를 사용 하 여 역참조 하 * (pr.첫 번째).액세스 하는 bool 구성 요소 쌍의 pr 멤버 함수에 의해 반환 된 사용 pr.second.
두 번째 삽입 멤버 함수, 힌트 버전 새 요소 지도에 삽입 된 위치를 가리키는 반복기를 반환 합니다.
마지막으로 두 명의 멤버 함수를 제외 하 고 처음 두와 똑같이 동작 _Val 삽입 된 값을 생성 하는 데 사용 됩니다.
설명
Value_type 요소의 값 정렬 된 쌍의 키 값과 같은 첫 번째 구성 요소와 다른 구성 요소에 데이터 값과 같은 수 있도록 요소를 한 쌍입니다.
삽입 하면 발생할 수 있습니다 힌트 버전 삽입 로그 시간 대신에 amortized 상수 시간에 바로 삽입 지점 다음에 오는 _Where.
반복기의 범위에 의해 주소가 지정 된 각 요소에 해당 하는 지도 요소 값 시퀀스의 세 번째 멤버 함수 삽입 [_First, _Last) 지정 된 집합입니다.
예제
// map_insert.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
#include <string>
int main( ) {
using namespace std;
map <int, int>::iterator m1_pIter, m2_pIter;
map <int, int> m1, m2;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 2, 20 ) );
m1.insert ( Int_Pair ( 3, 30 ) );
m1.insert ( Int_Pair ( 4, 40 ) );
cout << "The original key values of m1 =";
for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
cout << " " << m1_pIter -> first;
cout << "." << endl;
cout << "The original mapped values of m1 =";
for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
cout << " " << m1_pIter -> second;
cout << "." << endl;
pair< map<int,int>::iterator, bool > pr;
pr = m1.insert ( Int_Pair ( 1, 10 ) );
if( pr.second == true ) {
cout << "The element 10 was inserted in m1 successfully." << endl;
}
else {
cout << "Key number 1 already exists in m1\n"
<< "with an associated value of ( (pr.first) -> second ) = "
<< ( pr.first ) -> second
<< "." << endl;
}
// The hint version of insert
m1.insert( --m1.end( ), Int_Pair ( 5, 50 ) );
cout << "After the insertions, the key values of m1 =";
for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
cout << " " << m1_pIter -> first;
cout << "," << endl;
cout << "and the mapped values of m1 =";
for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
cout << " " << m1_pIter -> second;
cout << "." << endl;
m2.insert ( Int_Pair ( 10, 100 ) );
// The templatized version inserting a range
m2.insert( ++m1.begin( ), --m1.end( ) );
cout << "After the insertions, the key values of m2 =";
for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
cout << " " << m2_pIter -> first;
cout << "," << endl;
cout << "and the mapped values of m2 =";
for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
cout << " " << m2_pIter -> second;
cout << "." << endl;
// The templatized versions move constructing elements
map<int, string> m3, m4;
pair<int, string> is1(1, "a"), is2(2, "b");
m3.insert(move(is1));
cout << "After the move insertion, m3 contains:" << endl
<< " " << m3.begin()->first
<< " => " << m3.begin()->second
<< endl;
m4.insert(c4.begin(),move(is2));
cout << "After the move insertion, m4 contains:" << endl
<< " " << m4.begin()->first
<< " => " << m4.begin()->second
<< endl;
}
Output
The original key values of m1 = 1 2 3 4.
The original mapped values of m1 = 10 20 30 40.
Key number 1 already exists in m1
with an associated value of ( (pr.first) -> second ) = 10.
After the insertions, the key values of m1 = 1 2 3 4 5,
and the mapped values of m1 = 10 20 30 40 50.
After the insertions, the key values of m2 = 2 3 4 10,
and the mapped values of m2 = 20 30 40 100.
After the move insertion, m3 contains:
1 => a
After the move insertion, m4 contains:
2 => b
요구 사항
헤더: <map>
네임 스페이스: std