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")));
//使用对的转换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 << " => " << p.second << '\n';
}
}
输出:
a => a b => abcd c => cccccccccc d => ddd
在上面的示例中,通过分别向关键字和值传递构造函数参数将元素插入到map中。
让我们看一个插入元素的简单示例。
#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<<"输入家庭成员人数 :";
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 | Age \n ________________________\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
在上面的示例中,它只是根据用户的选择插入元素。