In an unordered_map, the key value is generally used to uniquely identify the element, while the mapped value is an object with the content associated to this key. Types of key and mapped value may differ. Internally, the elements in the unordered_map are not sorted in any particular order...
比较map、hash_map和unordered_map的执行效率以及内存占用情况 **/#include<sys/types.h>#include<unistd.h>#include<sys/time.h>#include<iostream>#include<fstream>#include<string>#include<map>#include<ext/hash_map>#include<tr1/unordered_map>usingnamespacestd;usingnamespace__gnu_cxx;usingnamespacestd...
比较map、hash_map和unordered_map的执行效率以及内存占用情况 **/#include<sys/types.h>#include<unistd.h>#include<sys/time.h>#include<iostream>#include<fstream>#include<string>#include<map>#include<ext/hash_map>#include<tr1/unordered_map>usingnamespacestd;usingnamespace__gnu_cxx;usingnamespacestd...
// swap (unordered_map specialization)#include <iostream>#include <string>#include <unordered_map>intmain () { std::unordered_map<std::string,std::string> first = {{"Star Wars","G. Lucas"},{"Alien","R. Scott"},{"Terminator","J. Cameron"}}, second = {{"Inception","C. Nolan"...
占用内存方面:hash_map内存占用最低,unordered_map其次,而map占用最高 stl::map [cpp]view plaincopy #include<string> #include<iostream> #include<map> usingnamespace std; struct person { string name; int age; person(string name,int age)
// swap (unordered_multimap specialization)#include <iostream>#include <string>#include <unordered_map>intmain () { std::unordered_multimap<std::string,std::string> a = {{"orange","FL"},{"apple","NY"},{"apple","WA"}}, b = {{"strawberry","LA"},{"strawberry","NC"},{"pear"...
boost::unordered_set can be replaced with std::unordered_set, boost::unordered_set doesn't differ from std::ordered_set. 2. boost::unordered_map #include <boost/unordered_map.hpp>#include<string>#include<iostream>intmain() { boost::unordered_map<std::string,int>map; ...
用法的区别就是,stl::map 的key需要定义operator< 。而boost::unordered_map需要定义hash_value函数并且重载operator==。对于内置类型,如string,这些都不用操心。对于自定义的类型做key,就需要自己重载operator< 或者hash_value()了。 最后,说,当不需要结果排好序时,最好用unordered_map。
// swap (unordered_set specialization)#include <iostream>#include <string>#include <unordered_set>intmain () { std::unordered_set<std::string> first = {"Metropolis","Solaris","Westworld"}, second = {"Avatar","Inception"}; swap(first,second); std::cout <<"first:";for(conststd::strin...
// swap (unordered_multiset specialization)#include <iostream>#include <string>#include <unordered_set>intmain () { std::unordered_multiset<std::string> first = {"cow","chicken","pig","pig"}, second = {"wolf","rabbit","rabbit"}; swap(first,second); std::cout <<"first:";for(cons...