1、定义
defined:Unordered maps are associative containers that store elements formed by the combination of a key value and a mapped value, and which allows for fast retrieval of individual elements based on their keys.
std::unordered_map<key,value> 变量名;(key:键的类型,value:值的类型)
2、capacity
2.1、empty
如果容器大小为0,则为true ,否则为false。
instance:
#include <iostream>
#include <unordered_map>
int main ()
{
std::unordered_map<int,int> first;
std::unordered_map<int,int> second = {{1,10},{2,20},{3,30}};
std::cout << "first " << (first.empty() ? "is empty" : "is not empty" ) << std::endl;
std::cout << "second " << (second.empty() ? "is empty" : "is not empty" ) << std::endl;
return 0;
}
#那啥三元运算符应该看得懂吧
2.2、size
返回容器中元素的数量。
eg:
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,double> mymap = {
{"milk",2.30},
{"potatoes",1.90},
{"eggs",0.40}
};
std::cout << "mymap.size() is " << mymap.size() << std::endl;
return 0;
}
2.3、max_size()
了解吧,这玩意很少用:
The maximum number of elements the object can hold as content.Member type?size_type?is an unsigned integral type.
3、Iterators
3.1、begin和end
#include <iostream>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string> mymap;
mymap = {{"Australia","Canberra"},{"U.S.","Washington"},{"UK","London"},{"France","Paris"},{"China","shanghai"}};
std::cout << "mymap contains:";
for(auto it = mymap.begin(); it!=mymap.end(); ++it )
std::cout << " " << it->first << ":" << it->second;
return 0;
}
4.Element access
4.1、[ ]
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string> mymap;
mymap["Bakery"]="Barbara"; // new element inserted
mymap["Seafood"]="Lisa"; // new element inserted
mymap["Produce"]="John"; // new element inserted
std::string name = mymap["Bakery"]; // existing element accessed (read)
mymap["Seafood"] = name; // existing element accessed (written)
mymap["Bakery"] = mymap["Produce"]; // existing elements accessed (read/written)
name = mymap["Deli"]; // non-existing element: new element "Deli" inserted!
mymap["Produce"] = mymap["Gifts"]; // new element "Gifts" inserted, "Produce" written
for (auto& x: mymap) {
std::cout << x.first << ": " << x.second << std::endl;
}
return 0;
}
4.2、at
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,int> mymap = {
{ "Mars", 3000},
{ "Saturn", 60000},
{ "Jupiter", 70000 } };
mymap.at("Mars") = 3396;
mymap.at("Saturn") += 272;
mymap.at("Jupiter") = mymap.at("Saturn") + 9638;
for (auto& x: mymap) {
std::cout << x.first << ": " << x.second << std::endl;
}
return 0;
}
5、Element lookup
5.1、find
Searches the container for an element with?k?as key and returns an iterator to it if found, otherwise it returns an iterator to?unordered_map::end?(the element past the end of the container).
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,double> mymap = {
{"mom",5.4},
{"dad",6.1},
{"bro",5.9} };
std::string input;
std::cout << "who? ";
getline (std::cin,input);
std::unordered_map<std::string,double>::const_iterator got = mymap.find (input);
if ( got == mymap.end() )
std::cout << "not found";
else
std::cout << got->first << " is " << got->second;
std::cout << std::endl;
return 0;
}
5.2、count
Searches the container for elements whose key is?k?and returns the number of elements found. Because?unordered_map?containers do not allow for duplicate keys, this means that the function actually returns?1?if an element with that key exists in the container, and zero otherwise.
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,double> mymap = {
{"Burger",2.99},
{"Fries",1.99},
{"Soda",1.50} };
for (auto& x: {"Burger","Pizza","Salad","Soda"}) {
if (mymap.count(x)>0)
std::cout << "mymap has " << x << std::endl;
else
std::cout << "mymap has no " << x << std::endl;
}
return 0;
}
6、Modifiers
6.1、clear
清空unordered_map。
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string> mymap =
{ {"house","maison"}, {"car","voiture"}, {"grapefruit","pamplemousse"} };
std::cout << "mymap contains:";
for (auto& x: mymap) std::cout << " " << x.first << "=" << x.second;
std::cout << std::endl;
mymap.clear();
mymap["hello"]="bonjour";
mymap["sun"]="soleil";
std::cout << "mymap contains:";
for (auto& x: mymap) std::cout << " " << x.first << "=" << x.second;
std::cout << std::endl;
return 0;
}
6.2、swap
交换两个unordered_map,不论大小是否相同,全部交换
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string>
first = {{"Star Wars","G. Lucas"},{"Alien","R. Scott"},{"Terminator","J. Cameron"}},
second = {{"Inception","C. Nolan"},{"Donnie Darko","R. Kelly"}};
first.swap(second);
std::cout << "first: ";
for (auto& x: first) std::cout << x.first << " (" << x.second << "), ";
std::cout << std::endl;
std::cout << "second: ";
for (auto& x: second) std::cout << x.first << " (" << x.second << "), ";
std::cout << std::endl;
return 0;
}
6.3、erase
Erase elements
Removes from the?unordered_map?container either a single element or a range of elements ([first,last)).
This effectively reduces the container?size?by the number of elements removed, calling each element's destructor.
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string> mymap;
// populating container:
mymap["U.S."] = "Washington";
mymap["U.K."] = "London";
mymap["France"] = "Paris";
mymap["Russia"] = "Moscow";
mymap["China"] = "Beijing";
mymap["Germany"] = "Berlin";
mymap["Japan"] = "Tokyo";
// erase examples:
mymap.erase ( mymap.begin() ); // erasing by iterator
mymap.erase ("France"); // erasing by key
mymap.erase ( mymap.find("China"), mymap.end() ); // erasing by range
// show content:
for ( auto& x: mymap )
std::cout << x.first << ": " << x.second << std::endl;
return
6.4、insert
Inserts new elements in the?unordered_map.
Each element is inserted only if its key is not equivalent to the key of any other element already in the container (keys in an?unordered_map?are unique).
This effectively increases the container?size?by the number of elements inserted.
The parameters determine how many elements are inserted and to which values they are initialized:
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,double>
myrecipe,
mypantry = {{"milk",2.0},{"flour",1.5}};
std::pair<std::string,double> myshopping ("baking powder",0.3);
myrecipe.insert (myshopping); // copy insertion
myrecipe.insert (std::make_pair<std::string,double>("eggs",6.0)); // move insertion
myrecipe.insert (mypantry.begin(), mypantry.end()); // range insertion
myrecipe.insert ( {{"sugar",0.8},{"salt",0.1}} ); // initializer list insertion
std::cout << "myrecipe contains:" << std::endl;
for (auto& x: myrecipe)
std::cout << x.first << ": " << x.second << std::endl;
std::cout << std::endl;
return 0;
}
6.5、emplace
Construct and insert element
Inserts a new element in the?unordered_map?if its key is unique. This new element is constructed in place using?args?as the arguments for the element's constructor.
The insertion only takes place if no element in the container has a key equivalent to the one being emplaced (keys in an?unordered_map?are unique).
If inserted, this effectively increases the container?size?by one.
A similar member function exists,?insert, which either copies or moves existing objects into the container.
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string> mymap;
mymap.emplace ("NCC-1701", "J.T. Kirk");
mymap.emplace ("NCC-1701-D", "J.L. Picard");
mymap.emplace ("NCC-74656", "K. Janeway");
std::cout << "mymap contains:" << std::endl;
for (auto& x: mymap)
std::cout << x.first << ": " << x.second << std::endl;
std::cout << std::endl;
return 0;
}
|