English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++ map emplace()函数用于通过在容器中插入新元素来扩展map容器。元素是直接构建的(既不复制也不移动)。
通过给传递给该函数的参数args调用元素的构造函数。仅当键不存在时才进行插入。
template <class...Args> pair<iterator, bool> emplace(Args&&... args); //从 C++ 11 开始
args:转发以构造要插入到map中的元素的参数。
它返回一个布尔对,它表示是否发生插入,并返回一个指向新插入元素的迭代器。
让我们看一个将元素插入map的简单示例。
#include <iostream> #include <map> using namespace std; int main(void) { map<char, int> m; m.emplace('a', 1); m.emplace('b', 2); m.emplace('c', 3); m.emplace('d', 4); m.emplace('e', 5); cout << "Map包含以下元素" << endl; for (auto it = m.begin(); it != m.end(); ++it) cout << it->first << " = " << it->second << endl; return 0; }
출력:
Map에 포함된 요소는 다음과 같습니다 a = 1 b = 2 c = 3 d = 4 e = 5
위의 예제에서, 주어진 키 값 쌍을 가진 요소가 map 컨테이너 m에 삽입되었습니다.
이제 간단한 예제를 보여드리겠습니다. 요소를 삽입하고 중복 키를 확인합니다.
#include <map> #include <string> #include <iostream> #include <string> using namespace std; template<typename M> void print(const M& m) { cout << m.size() << " 元素: "; for (const auto& p : m) { cout << "(" << p.first << ", " << p.second << ") "; } cout << endl; } int main() { map<int, string> m1; auto ret = m1.emplace(10, "ten"); ret = m1.emplace(20, "twenty"); ret = m1.emplace(30,"thirty"); if (!ret.second){ auto pr = *ret.first; cout << "Emplace 실패, 키10요소는 이미 존재합니다." << endl << "존재하는 요소는 (" << pr.first << ", " << pr.second << ")" << endl; cout << "map은 수정되지 않았습니다" << endl; } else{ cout << "map이 수정되었습니다, 새로운 요소 \n"; print(m1); } cout << endl; ret = m1.emplace(10, "one zero"); if (!ret.second){ auto pr = *ret.first; cout << "Emplace 실패, 키10요소는 이미 존재합니다." << endl << "존재하는 요소는 (" << pr.first << ", " << pr.second << ")" << endl; } else{ cout << "map이 수정되었습니다, 새로운 요소"; print(m1); } cout << endl; }
출력:
map이 수정되었습니다, 새로운 요소 3 요소: (10, ten) (20, twenty) (30, thirty) Emplace 실패, 키10요소는 이미 존재합니다. 존재하는 요소는 (10, ten)
위의 예제에서, 요소가 map에 삽입되었으며, 같은 키를 사용하여10그 때, 키에 대한 오류 메시지를 표시합니다10이미 존재합니다.
이제 간단한 예제를 보여드리겠습니다. map에 요소를 삽입하려면 생성자 매개변수를 키와 값으로 전달합니다.
#include <iostream> #include <utility> #include <string> using namespace std; #include <map> int main() { map<string, string> m; //pair의 move 생성자 사용 m.emplace(make_pair(string("a"), string("a"))); //pair의 move 생성자 사용 m.emplace(make_pair("b", "abcd")); //pair의 템플릿 생성자 사용 m.emplace("d", "ddd"); //pair의 부분 구조 함수 사용 m.emplace(piecewise_construct, forward_as_tuple("c"), forward_as_tuple(10, 'c')); for(const auto &p : m) { cout << p.first << " \t \t => \t " << p.second << '\n'; } }
출력:
a => a b => abcd c => ccccccccc d => ddd
위의 예제에서는 매핑에 각각 키와 값으로 생성자 파라미터를 전달하여 요소를 삽입합니다。
요소를 삽입하는 간단한 예제를 보겠습니다。
#include <iostream> #include <map> #include <string> using namespace std; int main() { typedef map<string, int> city; string name; int age; city fmly; int n; cout << "가족 구성원 수를 입력하세요: \n" cin >> n; cout << "각 구성원의 이름과 나이를 입력하세요: \n"; for(int i = 0; i < n; i++) { cin >> name; cin >> age; //fmly[name] = age; fmly.emplace(name, age); } cout << "\n가족 총 구성원은:" << fmly.size(); cout << "\n가족 구성원의 상세 정보: \n"; cout << "\nName \t \t Age \n \t________________________\n"; city::iterator p; for(p = fmly.begin(); p != fmly.end(); p++) { cout << (*p).first << " " << "|" << (*p).second << " \n "; } return 0; }
출력:
가족 구성원 수 입력: 3 각 구성원의 이름과 나이를 입력하세요: Ram 42 Sita 37 Laxman 40 가족 총 구성원은:3 가족 구성원의 상세 정보: Name | Age __________________________ Laxman | 40 Ram | 42 Sita | 37
위의 예제에서는 사용자 선택에 따라 요소를 삽입합니다.