multimap::insert
multimap에 요소 또는 요소의 범위를 삽입합니다.
// (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 |
multimap에 삽입할 요소의 값입니다. |
Where |
올바른 삽입 지점 검색을 시작할 위치입니다. 이 지점이 Where 바로 앞에 오면 로그 시간 대신 분할된 시간에 삽입할 수 있습니다. |
ValTy |
map이 value_type의 요소를 생성하는 데 사용할 수 있는 인수 형식을 지정하고 Val을 인수로 완벽하게 전달하는 템플릿 매개 변수입니다. |
First |
복사할 첫 번째 요소의 위치입니다. |
Last |
복사할 마지막 요소 바로 다음 위치입니다. |
InputIterator |
value_type 개체를 생성하는 데 사용할 수 있는 형식의 인수를 가리키는 입력 반복기의 요구 사항을 충족하는 템플릿 함수 인수입니다. |
IList |
요소를 복사해올 initializer_list입니다. |
반환 값
단일 요소 삽입 멤버 함수 (1) 및 (2)는 multimap에 새 요소를 삽입한 위치로 반복기를 반환합니다.
힌트가 있는 단일 요소 멤버 함수 (3) 및 (4)는 multimap에 새 요소를 삽입한 위치를 가리키는 반복기를 반환합니다.
설명
이 함수는 어떠한 포인터 또는 참조를 무효화하지 않지만 컨테이너에 대한 모든 반복기를 무효화할 수 있습니다.
요소를 한 개만 삽입하는 중 예외가 throw되면 컨테이너의 상태가 수정되지 않습니다. 여러 요소를 삽입하는 중 예외가 throw되면 컨테이너는 지정되지 않았으나 유효한 상태로 남아 있습니다.
컨테이너의 value_type은 컨테이너에 속한 typedef이고 맵의 경우 multimap<K, V>::value_type은 pair<const K, V>입니다. 요소의 값은 첫 번째 구성 요소가 키 값과 동일하고 두 번째 구성 요소는 요소의 데이터 값과 동일한 정렬된 쌍입니다.
범위 멤버 함수 (5)는 [First, Last) 범위에서 반복기가 주소를 지정하는 각 요소에 해당하는 multimap에 요소 값의 시퀀스를 입력하므로 Last는 삽입되지 않습니다. 컨테이너 멤버 함수 **end()**는 컨테이너의 마지막 요소 바로 뒤에 있는 위치를 참조합니다. 예를 들어 m.insert(v.begin(), v.end()); 문은 v의 모든 요소를 m에 삽입합니다.
이니셜라이저 목록 멤버 함수 (6)은 initializer_list를 사용하여 요소를 map으로 복사합니다.
생성된 요소를 제 위치에 삽입하려면, 즉 복사 또는 이동 작업을 수행하지 않으려면 multimap::emplace 및 multimap::emplace_hint를 참조하세요.
예제
// multimap_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
multimap<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
m1.insert(make_pair(1, 111));
cout << "The modified key and mapped values of m1 are:" << endl;
print(m1);
// 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
multimap<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
multimap<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;
multimap<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)
The modified key and mapped values of m1 are:
3 elements: (1, 10) (1, 111) (2, 20)
The modified key and mapped values of m1 are:
4 elements: (1, 10) (1, 111) (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