目錄
1.unordered_ map特性
2. 常用接口的使用
1.insert
???????? 2.find
3.erase
?4.operator[ ]
?3.迭代器的有效性
1.unordered_ map特性
1. unordered_map是存儲<key, value>鍵值對的關(guān)聯(lián)式容器,其允許通過keys快速的索引到與
其對應(yīng)的value。
2. 在unordered_map中,鍵值通常用于惟一地標識元素,而映射值是一個對象,其內(nèi)容與此
鍵關(guān)聯(lián)。鍵和映射值的類型可能不同。
3. 在內(nèi)部,unordered_map沒有對<kye, value>按照任何特定的順序排序, 為了能在常數(shù)范圍內(nèi)
找到key所對應(yīng)的value,unordered_map將相同哈希值的鍵值對放在相同的桶中。
4. unordered_map容器通過key訪問單個元素要比map快,但它通常在遍歷元素子集的范圍迭
代方面效率較低。
5. unordered_maps實現(xiàn)了直接訪問操作符(operator[]),它允許使用key作為參數(shù)直接訪問
value。
6. 它的迭代器至少是前向迭代器。
2. 常用接口的使用
1.insert
向unordered_map中插入新元素。每個元素的鍵與容器中其他元素的鍵不相等時才會被插入(unordered_map中的鍵是唯一的)。
插入元素的數(shù)量會有效地增加容器的大小。
插入范圍:[first, last),范圍包含first和last之間的所有元素,包括first指向的元素,但不包括last指向的元素。
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string, double> myrecipe, mypantry = { {"milk",2.0},{"flour",1.5} };
pair<string, double> myshopping("baking powder", 0.3);
myrecipe.insert(myshopping);
myrecipe.insert(make_pair<string, double>("eggs", 6.0));
myrecipe.insert(mypantry.begin(), mypantry.end());
myrecipe.insert({ {"sugar",0.8},{"salt",0.1} });
cout << "myrecipe contains:" << endl;
for (auto& x : myrecipe)
cout << x.first << ": " << x.second << endl;
cout << endl;
return 0;
}
2.find
獲取元素的迭代器
在容器中搜索鍵為k的元素,如果找到則返回指向該元素的迭代器,否則返回指向unordered_map::end(容器末尾之后的元素)的迭代器。
也可以使用or operator[]的成員函數(shù)直接訪問映射后的值。
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<std::string, double> mymap = {
{"mom",5.4},
{"dad",6.1},
{"bro",5.9} };
string input;
cout << "who? ";
getline(cin, input);
unordered_map<string, double>::const_iterator got = mymap.find(input);
if (got == mymap.end())
cout << "not found";
else
cout << got->first << " is " << got->second;
cout << endl;
return 0;
}
3.erase
從unordered_map容器中刪除單個元素或一組元素[first,last)。
返回一個迭代器,指向最后一個被擦除的元素之后的位置。?
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string, string> mymap;
// 填充容器
mymap["U.S."] = "Washington";
mymap["U.K."] = "London";
mymap["France"] = "Paris";
mymap["Russia"] = "Moscow";
mymap["China"] = "Beijing";
mymap["Germany"] = "Berlin";
mymap["Japan"] = "Tokyo";
// 進行刪除
mymap.erase(mymap.begin()); // 通過迭代器刪除
mymap.erase("France"); // 通過key刪除
mymap.erase(mymap.find("China"), mymap.end()); // 通過范圍進行刪除
for (auto& x : mymap)
cout << x.first << ": " << x.second << endl;
return 0;
}
?4.operator[ ]
訪問元素
如果k與容器中元素的鍵匹配,則該函數(shù)返回其映射值的引用。
如果k與容器中任何元素的鍵都不匹配,該函數(shù)將插入一個具有該鍵的新元素,并返回其映射值的引用。注意,即使沒有映射值給元素(元素是使用其默認構(gòu)造函數(shù)構(gòu)造的),這樣做也會使容器大小增加1。
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string, string> mymap;
mymap["Bakery"] = "Barbara"; // 插入新元素
mymap["Seafood"] = "Lisa";
mymap["Produce"] = "John";
string name = mymap["Bakery"];
mymap["Seafood"] = name;
mymap["Bakery"] = mymap["Produce"];
name = mymap["Deli"];
mymap["Produce"] = mymap["Gifts"];
for (auto& x : mymap) {
cout << x.first << ": " << x.second << endl;
}
return 0;
}
文章來源:http://www.zghlxwxcb.cn/news/detail-704778.html
?3.迭代器的有效性
在大多數(shù)情況下,插入后容器中的所有迭代器仍然有效。唯一的例外是當(dāng)此函數(shù)插入新元素時,這將強制重新散列(rehash)。在這種情況下,容器中的所有迭代器都失效。如果插入操作后的新容器大小超過其容量閾值則強制進行重新散列。文章來源地址http://www.zghlxwxcb.cn/news/detail-704778.html
到了這里,關(guān)于C++:關(guān)聯(lián)式容器:unordered_map的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!