C++ Multimap emplace()函数用于通过在集合中插入新元素来扩展multimap集合。
通过给传递给该函数的参数args调用元素的构造函数。
template <class... Args> iterator emplace (Args&&... args); //since C++ 11
args :转发的参数用于构造要插入multimap的元素。
C++ emplace()函数指示是否发生了插入,并返回指向新插入元素的迭代器。
让我们看一个将元素插入到multimap的简单示例:
#include <iostream> #include <map> using namespace std; int main(void) { multimap<char, int> m; m.emplace('a', 1); m.emplace('b', 2); m.emplace('c', 3); m.emplace('b', 4); m.emplace('c', 5); cout << "Multimap contains following elements" << endl; for (auto it = m.begin(); it != m.end(); ++it) cout << it->first << " = " << it->second << endl; return 0; }
输出:
Multimap contains following elements a = 1 b = 2 b = 4 c = 3 c = 5
在上面的示例中,它只是将具有给定键值对的元素插入到multimapm中。
让我们看一个简单的示例,插入元素并检查在multimap中是否允许重复键:
#include <map> #include <string> #include <iostream> using namespace std; template <typename M> void print(const M& m) { cout << m.size() << " elements: " << endl; for (const auto& p : m) { cout << "(" << p.first << "," << p.second << ") "; } cout << endl; } int main() { multimap<string, string> m1; m1.emplace("Nikita", "Accounting"); m1.emplace("Amita", "Accounting"); m1.emplace("Deep", "Engineering"); cout << "multimap modified, now contains "; print(m1); cout << endl; m1.emplace("Nikita", "Engineering"); cout << "multimap modified, now contains "; print(m1); cout << endl; }
输出:
multimap modified, now contains 3 elements: (Amita,Accounting) (Deep,Engineering) (Nikita,Accounting) multimap modified, now contains 4 elements: (Amita,Accounting) (Deep,Engineering) (Nikita,Accounting) (Nikita,Engineering)
在上面的示例中,元素被插入了multimap,当您尝试添加相同的键Nikita时,它将允许您插入重复项。
让我们看一个简单的示例,分别通过将构造函数参数传递给键和值来将元素插入multimap:
链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-multimap-emplace-function.html
来源:LearnFk无涯教程网
#include <iostream> #include <utility> #include <string> #include <map> using namespace std; int main() { multimap<string, string> m; //uses pair's move constructor m.emplace(make_pair(string("a"), string("a"))); //uses pair's converting move constructor m.emplace(make_pair("b", "abcd")); //uses pair's template constructor m.emplace("a", "aaa"); //uses pair's piecewise constructor m.emplace(piecewise_construct, forward_as_tuple("c"), forward_as_tuple(10, 'c')); for (const auto &p : m) { cout << p.first << " => " << p.second << '\n'; } }
输出:
a => a a => aaa b => abcd c => cccccccccc
在上面的示例中,通过将构造函数参数分别传递给键和值,将元素插入到multimap中。
让我们看一个插入元素的简单示例:
#include <iostream> #include <map> #include <string> using namespace std; int main() { typedef multimap<string, int> city; string name; int age; city fmly ; int n; cout<<"Enter the number of fmly members :"; cin>>n; cout<<"Enter the name and age of each member: \n"; for(int i =0; i<n; i++) { cin>> name; //Get key cin>> age; //Get value fmly.emplace(name,age);//Put them in multimap } cout<<"\nTotal memnber of fmly is:"<< fmly.size(); cout<<"\nDetails of fmly members: \n"; cout<<"\nName | Age \n ________________________\n"; city::iterator p; for(p = fmly.begin(); p!=fmly.end(); p++) { cout<<(*p).first << " | " <<(*p).second <<" \n "; } return 0; }
输出:
Enter the number of fmly members : 3 Enter the name and age of each member: Ram 42 Sita 37 Laxman 40 Total memnber of fmly is:3 Details of fmly members: Name | Age __________________________ Laxman | 40 Ram | 42 Sita | 37
在上面的示例中,它只是根据用户的选择插入元素。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)