First, let's slightly modify the insert_numbers function in the blog. We'll divide the function into the construction part and the testing part, so that we can easily use only the construction part when we actually hack. Note that unordered_map and unordered_set work exactly the same for ...
#include <iostream>#include <map>int main() {// 创建并初始化一个mapstd::map<std::string, int> m = { {"Alice", 25}, {"Bob", 22}, {"Charlie", 30} };// 插入元素// std::pair<iterator,bool> insert (const value_type& val);m.insert(std::make_pair("David", 32));// 查找...
std::unordered_mapis an associative container that contains key-value pairs with unique keys. Search, insertion, and removal of elements have average constant-time complexity. Internally, the elements are not sorted in any particular order, but organized into buckets. Which bucket an element is pl...
// unordered_map::cbegin/cend example#include <iostream>#include <unordered_map>intmain () { std::unordered_map<std::string,std::string> mymap; mymap = {{"Australia","Canberra"},{"U.S.","Washington"},{"France","Paris"}}; std::cout <<"mymap contains:";for(autoit = mymap.cbe...
// unordered_map::empty#include <iostream>#include <unordered_map>intmain () { 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::cou...
MapKey MyMap; gettimeofday(&begin,NULL);for(inti=0;i<N;++i) MyMap.insert(make_pair(i,i)); gettimeofday(&end,NULL); cout<<"insert N="<<N<<",cost="<<end.tv_sec-begin.tv_sec +float(end.tv_usec-begin.tv_usec)/1000000<<"sec"<<endl;for(inti=0;i<N;++i) ...
MapKey MyMap; gettimeofday(&begin,NULL);for(inti=0;i<N;++i) MyMap.insert(make_pair(i,i)); gettimeofday(&end,NULL); cout<<"insert N="<<N<<",cost="<<end.tv_sec-begin.tv_sec +float(end.tv_usec-begin.tv_usec)/1000000<<"sec"<<endl;for(inti=0;i<N;++i) ...
// unordered_multimap comparisons#include <iostream>#include <string>#include <unordered_map>typedefstd::unordered_multimap<std::string,int> stringmap;intmain () { stringmap a = { {"AAPL",100}, {"MSFT",200}, {"GOOG",50}, {"GOOG",20} }; stringmap b = { {"GOOG",20}, {"MSFT...
// swap (unordered_multimap specialization) #include <iostream> #include <string> #include <unordered_map> int main () { std::unordered_multimap<std::string,std::string> a = {{"orange","FL"},{"apple","NY"},{"apple","WA"}}, b = {{"strawberry","LA"},{"strawberry","NC"},{...
cout <<"\nFirst6 set: ";for(conststring& x: first6 ){ cout <<" "<< x; }return0;/** other function please to see the unordered_map */ Reference cplusplus