does there exist a kind of "inverse" associative container in the stl or in general? For instance I'd like to have a container where the same element is shared by a set of keys.
Let's say my key is an int
, then I would have for instance:
container.at(3) -> some object A
container.at(4) -> same object A
container.at(1) -> other object B
This container would have (ideally) the same complexity as std::map for the different operations. Is such a thing possible?
I was thinking about using at first a std::map<int, T*>
where several indexes point to the same object, but then when removing an item from the map, the running time is in O(n) because you'd have to check the other items to see if you need to delete the T object
Is there already this kind of container "natively" in the stl or in boost?
edit : some example of utilisation:
container<int, myClass> myContainer;
myClass obj(...); //new object
myContainer.insert(3, obj); //insert object for specific key
myContainer.insert(2, obj); //insert same object for specific key, since both objects would compare equal we would actually not "insert" a new object but have it shared by both keys
myContainer.duplicate_object(2,5); //key 5 also has the same object as key 2 (and 3)
myContainer.getAllKeys(2); //would return 2,3 and 5 since they all reference the same object as key 2
myContainer.removeKey(3);
myContainer.removeKey(2);
myContainer.removeKey(5); //would destroy the object here, not before