C ++ map emplace()函數(shù)用于通過在容器中插入新元素來擴(kuò)展map容器。元素是直接構(gòu)建的(既不復(fù)制也不移動)。
通過給傳遞給該函數(shù)的參數(shù)args調(diào)用元素的構(gòu)造函數(shù)。僅當(dāng)鍵不存在時才進(jìn)行插入。
template <class...Args> pair<iterator, bool> emplace (Args&&... args); //從 C++ 11 開始
args:轉(zhuǎn)發(fā)以構(gòu)造要插入到map中的元素的參數(shù)。
它返回一個布爾對,它表示是否發(fā)生插入,并返回一個指向新插入元素的迭代器。
讓我們看一個將元素插入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中。
讓我們看一個簡單的示例,插入元素并檢查重復(fù)鍵。
#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的元素已經(jīng)存在。"
<< 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的元素已經(jīng)存在。"
<< endl << "現(xiàn)有元素是 (" << pr.first << ", " << pr.second << ")"
<< endl;
}
else{
cout << "map 被修改,新的的元素";
print(m1);
}
cout << endl;
}輸出:
map 被修改,新的的元素 3 元素: (10, ten) (20, twenty) (30, thirty) Emplace失敗,鍵10的元素已經(jīng)存在。 現(xiàn)有元素是 (10, ten)
在上面的示例中,元素插入了map,并且當(dāng)您嘗試使用相同的鍵10時,它將顯示一條錯誤消息,提示鍵10已經(jīng)存在。
讓我們看一個簡單的示例,分別通過將構(gòu)造函數(shù)參數(shù)傳遞給鍵和值,將元素插入map。
#include <iostream>
#include <utility>
#include <string>
using namespace std;
#include <map>
int main()
{
map<string, string> m;
//使用pair的move構(gòu)造函數(shù)
m.emplace(make_pair(string("a"), string("a")));
//使用對的轉(zhuǎn)換move構(gòu)造函數(shù)
m.emplace(make_pair("b", "abcd"));
//使用pair的模板構(gòu)造函數(shù)
m.emplace("d", "ddd");
//使用pair的分段構(gòu)造函數(shù)
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
在上面的示例中,通過分別向關(guān)鍵字和值傳遞構(gòu)造函數(shù)參數(shù)將元素插入到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<<"輸入家庭成員人數(shù) :";
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家庭成員的詳細(xì)信息: \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;
}輸出:
輸入家庭成員人數(shù) : 3 輸入每個成員的姓名和年齡: Ram 42 Sita 37 Laxman 40 家庭總成員是:3 家庭成員的詳細(xì)信息: Name | Age __________________________ Laxman | 40 Ram | 42 Sita | 37
在上面的示例中,它只是根據(jù)用戶的選擇插入元素。