#include <iostream>
#include <map>
template <typename MapType>
class map_add_values {
private:
MapType mMap;
public:
typedef typename MapType::key_type KeyType;
typedef typename MapType::mapped_type MappedType;
map_add_values(const KeyType& key, const MappedType& val)
{
mMap[key] = val;
}
map_add_values& operator()(const KeyType& key, const MappedType& val) {
mMap[key] = val;
return *this;
}
void to (MapType& map) {
map.insert(mMap.begin(), mMap.end());
}
};
typedef std::map<int, int> Int2IntMap;
void printMap(const Int2IntMap& map) {
std::cout << "Contents of Map" << std::endl;
std::cout << "===============" << std::endl;
Int2IntMap::const_iterator mapIterator = map.begin(),
mapIteratorEnd = map.end();
while (mapIterator != mapIteratorEnd) {
std::cout << "(" << mapIterator->first << ", " << mapIterator->second << ")" << std::endl;
++mapIterator;
}
std::cout << std::endl;
}
int main() {
Int2IntMap testMap;
map_add_values<Int2IntMap>(1,2)(3,4)(5,6).to(testMap);
printMap(testMap);
map_add_values<Int2IntMap>(10,20)(30,40)(50,60).to(testMap);
printMap(testMap);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWFwPgoKdGVtcGxhdGUgPHR5cGVuYW1lIE1hcFR5cGU+CmNsYXNzIG1hcF9hZGRfdmFsdWVzIHsKcHJpdmF0ZToKICAgIE1hcFR5cGUgbU1hcDsKcHVibGljOgogICAgdHlwZWRlZiB0eXBlbmFtZSBNYXBUeXBlOjprZXlfdHlwZSBLZXlUeXBlOwogICAgdHlwZWRlZiB0eXBlbmFtZSBNYXBUeXBlOjptYXBwZWRfdHlwZSBNYXBwZWRUeXBlOwogICAgCiAgICBtYXBfYWRkX3ZhbHVlcyhjb25zdCBLZXlUeXBlJiBrZXksIGNvbnN0IE1hcHBlZFR5cGUmIHZhbCkKICAgIHsKICAgICAgICBtTWFwW2tleV0gPSB2YWw7CiAgICB9CiAgICAKICAgIG1hcF9hZGRfdmFsdWVzJiBvcGVyYXRvcigpKGNvbnN0IEtleVR5cGUmIGtleSwgY29uc3QgTWFwcGVkVHlwZSYgdmFsKSB7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sKfQ==
Contents of Map
===============
(1, 2)
(3, 4)
(5, 6)
Contents of Map
===============
(1, 2)
(3, 4)
(5, 6)
(10, 20)
(30, 40)
(50, 60)