fork download
  1. // constructing unordered_maps
  2. #include <iostream>
  3. #include <string>
  4. #include <unordered_map>
  5.  
  6. typedef std::unordered_map<std::string,std::string> stringmap;
  7.  
  8. stringmap merge (stringmap a,stringmap b) {
  9. stringmap temp(a); temp.insert(b.begin(),b.end()); return temp;
  10. }
  11.  
  12. int main ()
  13. {
  14. stringmap first; // empty
  15. stringmap second ( {{"apple","red"},{"lemon","yellow"}} ); // init list
  16. stringmap third ( {{"orange","orange"},{"strawberry","red"}} ); // init list
  17. stringmap fourth (second); // copy
  18. stringmap fifth (merge(third,fourth)); // move
  19. stringmap sixth (fifth.begin(),fifth.end()); // range
  20.  
  21. std::cout << "sixth contains:";
  22. for (auto& x: sixth) std::cout << " " << x.first << ":" << x.second;
  23. std::cout << std::endl;
  24.  
  25. return 0;
  26. }
Success #stdin #stdout 0s 2996KB
stdin
Standard input is empty
stdout
sixth contains: orange:orange lemon:yellow strawberry:red apple:red