fork(5) download
  1. #include <iostream>
  2. #include <map>
  3.  
  4. template <typename MapType>
  5. class map_add_values {
  6. private:
  7. MapType mMap;
  8. public:
  9. typedef typename MapType::key_type KeyType;
  10. typedef typename MapType::mapped_type MappedType;
  11.  
  12. map_add_values(const KeyType& key, const MappedType& val)
  13. {
  14. mMap[key] = val;
  15. }
  16.  
  17. map_add_values& operator()(const KeyType& key, const MappedType& val) {
  18. mMap[key] = val;
  19. return *this;
  20. }
  21.  
  22. void to (MapType& map) {
  23. map.insert(mMap.begin(), mMap.end());
  24. }
  25. };
  26.  
  27. typedef std::map<int, int> Int2IntMap;
  28.  
  29. void printMap(const Int2IntMap& map) {
  30. std::cout << "Contents of Map" << std::endl;
  31. std::cout << "===============" << std::endl;
  32. Int2IntMap::const_iterator mapIterator = map.begin(),
  33. mapIteratorEnd = map.end();
  34. while (mapIterator != mapIteratorEnd) {
  35. std::cout << "(" << mapIterator->first << ", " << mapIterator->second << ")" << std::endl;
  36. ++mapIterator;
  37. }
  38. std::cout << std::endl;
  39. }
  40.  
  41. int main() {
  42. Int2IntMap testMap;
  43. map_add_values<Int2IntMap>(1,2)(3,4)(5,6).to(testMap);
  44. printMap(testMap);
  45. map_add_values<Int2IntMap>(10,20)(30,40)(50,60).to(testMap);
  46. printMap(testMap);
  47. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
Contents of Map
===============
(1, 2)
(3, 4)
(5, 6)

Contents of Map
===============
(1, 2)
(3, 4)
(5, 6)
(10, 20)
(30, 40)
(50, 60)