map::insert
map에 요소 또는 요소의 범위를 삽입합니다.
// (1) single element pair<iterator, bool> insert( const value_type& Val ); // (2) single element, perfect forwarded template<class ValTy> pair<iterator, bool> insert( ValTy&& Val ); // (3) single element with hint iterator insert( const_iterator Where, const value_type& Val ); // (4) single element, perfect forwarded, with hint template<class ValTy> iterator insert( const_iterator Where, ValTy&& Val ); // (5) range template<class InputIterator> void insert( InputIterator First, InputIterator Last ); // (6) initializer list void insert( initializer_list<value_type> IList );
매개 변수
매개 변수 |
설명 |
Val |
키가 동등하게 정렬된 요소가 이미 포함되어 있지 않으면 map에 삽입되는 요소의 값입니다. |
Where |
올바른 삽입 지점 검색을 시작할 위치입니다. 이 지점이 Where 바로 앞에 오면 로그 시간 대신 분할된 시간에 삽입할 수 있습니다. |
ValTy |
map이 value_type의 요소를 생성하는 데 사용할 수 있는 인수 형식을 지정하고 Val을 인수로 완벽하게 전달하는 템플릿 매개 변수입니다. |
First |
복사할 첫 번째 요소의 위치입니다. |
Last |
복사할 마지막 요소 바로 다음 위치입니다. |
InputIterator |
value_type 개체를 생성하는 데 사용할 수 있는 형식의 인수를 가리키는 입력 반복기의 요구 사항을 충족하는 템플릿 함수 인수입니다. |
IList |
요소를 복사해올 initializer_list입니다. |
반환 값
단일 요소 멤버 함수 (1) 및 (2)는 bool 구성 요소가 삽입된 경우 true이고 map에 배열 시 키에 동일한 값이 있는 요소가 이미 포함되어 있는 경우 flase인 쌍을 반환합니다. 반환 값 쌍의 반복기 구성 요소는 bool 구성 요소가 true인 경우에는 새로 삽입된 요소를 가리키고 bool 구성 요소가 false인 경우에는 기존 요소를 가리킵니다.
힌트가 있는 단일 요소 멤버 함수 (3) 및 (4)는 새 요소가 map에 삽입된 위치를 가리키는 반복기를 반환하고 동일한 키가 있는 요소가 존재하는 경우에는 기존 요소를 가리키는 반복기를 반환합니다.
설명
이 함수는 반복기, 포인터 또는 참조를 무효화하지 않습니다.
요소를 한 개만 삽입하는 중 예외가 throw되면 컨테이너의 상태가 수정되지 않습니다. 여러 요소를 삽입하는 중 예외가 throw되면 컨테이너는 지정되지 않았으나 유효한 상태로 남아 있습니다.
단일 요소 멤버 함수에서 반환한 pair pr의 반복기 구성 요소에 액세스하려면 pr.first를 사용하고 반환된 쌍 내에서 반복기를 역참조하려면 *pr.first를 사용하여 요소를 제공합니다. bool 구성 요소에 액세스하려면 pr.second를 사용합니다. 예제는 이 문서 뒷부분에 있는 샘플 코드를 참조하세요.
컨테이너의 value_type은 컨테이너에 속한 typedef이고 맵의 경우 map<K, V>::value_type은 pair<const K, V>입니다. 요소의 값은 첫 번째 구성 요소가 키 값과 동일하고 두 번째 구성 요소는 요소의 데이터 값과 동일한 정렬된 쌍입니다.
범위 멤버 함수 (5)는 [First, Last) 범위에서 반복기가 주소를 지정하는 각 요소에 해당하는 map에 요소 값의 시퀀스를 입력하므로 Last는 삽입되지 않습니다. 컨테이너 멤버 함수 **end()**는 컨테이너의 마지막 요소 바로 뒤에 있는 위치를 참조합니다. 예를 들어 m.insert(v.begin(), v.end()); 문이 v의 모든 요소를 m에 삽입하려고 합니다. 범위에 고유 값이 있는 요소만 삽입됩니다. 중복 값은 무시됩니다. 어떤 요소가 거부되는지 관찰하려면 insert의 단일 요소 버전을 사용합니다.
이니셜라이저 목록 멤버 함수 (6)은 initializer_list를 사용하여 요소를 map으로 복사합니다.
생성된 요소를 제 위치에 삽입하려면, 즉 복사 또는 이동 작업을 수행하지 않으려면 map::emplace 및 map::emplace_hint를 참조하세요.
예제
// map_insert.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
#include <string>
#include <vector>
#include <utility> // make_pair()
using namespace std;
template <typename M> void print(const M& m) {
cout << m.size() << " elements: ";
for (const auto& p : m) {
cout << "(" << p.first << ", " << p.second << ") ";
}
cout << endl;
}
int main()
{
// insert single values
map<int, int> m1;
// call insert(const value_type&) version
m1.insert({ 1, 10 });
// call insert(ValTy&&) version
m1.insert(make_pair(2, 20));
cout << "The original key and mapped values of m1 are:" << endl;
print(m1);
// intentionally attempt a duplicate, single element
auto ret = m1.insert(make_pair(1, 111));
if (!ret.second){
auto pr = *ret.first;
cout << "Insert failed, element with key value 1 already exists."
<< endl << " The existing element is (" << pr.first << ", " << pr.second << ")"
<< endl;
}
else{
cout << "The modified key and mapped values of m1 are:" << endl;
print(m1);
}
cout << endl;
// single element, with hint
m1.insert(m1.end(), make_pair(3, 30));
cout << "The modified key and mapped values of m1 are:" << endl;
print(m1);
cout << endl;
// The templatized version inserting a jumbled range
map<int, int> m2;
vector<pair<int, int>> v;
v.push_back(make_pair(43, 294));
v.push_back(make_pair(41, 262));
v.push_back(make_pair(45, 330));
v.push_back(make_pair(42, 277));
v.push_back(make_pair(44, 311));
cout << "Inserting the following vector data into m2:" << endl;
print(v);
m2.insert(v.begin(), v.end());
cout << "The modified key and mapped values of m2 are:" << endl;
print(m2);
cout << endl;
// The templatized versions move-constructing elements
map<int, string> m3;
pair<int, string> ip1(475, "blue"), ip2(510, "green");
// single element
m3.insert(move(ip1));
cout << "After the first move insertion, m3 contains:" << endl;
print(m3);
// single element with hint
m3.insert(m3.end(), move(ip2));
cout << "After the second move insertion, m3 contains:" << endl;
print(m3);
cout << endl;
map<int, int> m4;
// Insert the elements from an initializer_list
m4.insert({ { 4, 44 }, { 2, 22 }, { 3, 33 }, { 1, 11 }, { 5, 55 } });
cout << "After initializer_list insertion, m4 contains:" << endl;
print(m4);
cout << endl;
}
출력
The original key and mapped values of m1 are:
2 elements: (1, 10) (2, 20)
Insert failed, element with key value 1 already exists.
The existing element is (1, 10)
The modified key and mapped values of m1 are:
3 elements: (1, 10) (2, 20) (3, 30)
Inserting the following vector data into m2:
5 elements: (43, 294) (41, 262) (45, 330) (42, 277) (44, 311)
The modified key and mapped values of m2 are:
5 elements: (41, 262) (42, 277) (43, 294) (44, 311) (45, 330)
After the first move insertion, m3 contains:
1 elements: (475, blue)
After the second move insertion, m3 contains:
2 elements: (475, blue) (510, green)
After initializer_list insertion, m4 contains:
5 elements: (1, 11) (2, 22) (3, 33) (4, 44) (5, 55)
요구 사항
헤더: <map>
네임스페이스: std