C++ Map emplace()
C++ Map emplace()函数
C++地图 emplace()函数用于通过插入来扩展地图容器新元素放入容器中。元素是直接构建的(既不能复制也不能移动)。
通过将参数args传递给该函数来调用元素的构造函数。仅当密钥不存在时才进行插入。
语法
template <class...Args>
pair<iterator, bool> emplace (Args&&... args); //since C++ 11
参数
参数: 为构造要插入到地图中的元素而转发的参数。
返回值
它返回布尔对,该布尔对将指示是否发生插入,并返回指向新插入元素的迭代器。
示例1
让我们看一个将元素插入地图的简单示例。
#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 contains following elements" << endl;
for (auto it = m.begin(); it != m.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
输出:
Map contains following elements
a = 1
b = 2
c = 3
d = 4
e = 5
在上面的示例中,它只是将具有给定键值对的元素插入到映射m中。
示例2
让我们来看一个简单的例子插入元素并检查重复键的示例。
#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");
ret = m1.emplace(20, "twenty");
ret= m1.emplace(30,"thirty");
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 \n";
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;
}
输出:
map modified, now contains
3 elements: (10, ten) (20, twenty) (30, thirty)
Emplace failed, element with key 10 already exists.
The existing element is (10, ten)
在上面的示例中,元素插入了地图,并且当您尝试使用相同的键10时,它将显示一条错误消息,提示键10已经存在。
示例3
让我们看一个简单的示例,通过将构造函数参数分别传递给键和值来将元素插入地图。
#include <iostream>
#include <utility>
#include <string>
using namespace std;
#include <map>
int main()
{
map<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("d", "ddd");
// 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
b => abcd
c => cccccccccc
d => ddd
在上面的示例中,通过分别向键和值传递构造函数参数将元素插入到地图中。
示例4
看到一个简单的示例来插入元素。
#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<<"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[name] = age; // Put them in map
fmly.emplace(name,age);
}
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
在上面的示例中,它只是根据用户的选择插入元素。