map::emplace
지도로 (복사 또는 이동 작업이 수행되지 않은)장소에 생성 된 요소를 삽입합니다.
template<class... Args>
pair<iterator, bool> emplace(
Args&&... args);
매개 변수
Parameter |
설명 |
args |
구성 요소와 동일한 값을 갖는 순서가 포함 되어 있지 않으면, 인수는 지도에 삽입 될 요소를 구문으로 전달합니다. |
반환 값
삽입 될 것이 만들어져 있다면, bool 구성요소의 쌍 은 true이고, 지도에 순서에 해당 하는 값의 요소를 이미 포함되어 있다면 false입니다. bool 구성요소가 true이면, 반환 값 쌍의 반복기 구성 요소는 새롭게 삽입된 요소를 가리키고, bool 구성 요소가 false이면 기존의 요소를 가리킵니다.
pair pr 의 반복기 구성요소를 액세스 하려면, pr.first 을 사용하고, 이것을 역참조하려면, *pr.first 를 사용합니다. bool 구성요소에 접근하려면, pr.second를 사용합니다. 예를 들어,이 문서의 뒷부분에 나오는 예제 코드를 참조 하십시오.
설명
이 함수에 의해 참조가 없거나 반복기가 무효화 됩니다.
Emplacement하는 동안, 예외가 나타나면, 컨테이너의 상태는 수정되지 않습니다.
요소의 값을 키 값과 같은 첫 번째 구성 요소와 요소의 데이터 값과 같은 두 번째 구성 요소를 사용하여 정렬된 된 쌍이 될 수 있도록 요소의 value_type 는 한 쌍으로 되어있습니다.
예제
// map_emplace.cpp
// compile with: /EHsc
#include <map>
#include <string>
#include <iostream>
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()
{
map<int, string> m1;
auto ret = m1.emplace(10, "ten");
if (!ret.second){
auto pr = *ret.first;
cout << "Emplace failed, element with key 10 already exists."
<< endl << " The existing element is (" << pr.first << ", " << pr.second << ")"
<< endl;
cout << "map not modified" << endl;
}
else{
cout << "map modified, now contains ";
print(m1);
}
cout << endl;
ret = m1.emplace(10, "one zero");
if (!ret.second){
auto pr = *ret.first;
cout << "Emplace failed, element with key 10 already exists."
<< endl << " The existing element is (" << pr.first << ", " << pr.second << ")"
<< endl;
}
else{
cout << "map modified, now contains ";
print(m1);
}
cout << endl;
}
Output
map modified, now contains 1 elements: (10, ten)
Emplace failed, element with key 10 already exists.
The existing element is (10, ten)
요구 사항
헤더: <맵>
네임스페이스: std