user541686
user541686

Reputation: 210402

How to have: map<T, vector<iterator to map itself> >?

I need a map whose keys are of some composite type T to a vector of iterators of the map itself.
(e.g., think of a graph in which every node holds iterators that point to its parents.)

The reason I'm avoiding storing the parents' keys as values is that the keys are expensive objects, so I'd like to store iterators instead.

Is such a thing possible?
If not, what's the best alternative?

(I know I can use polymorphism and type erasure as a brute-force way to solve this, but I'm wondering if there's a better way.)

Upvotes: 3

Views: 458

Answers (2)

user2249683
user2249683

Reputation:

Due to 23.2.1 General container requirements:

Containers are objects that store other objects. They control allocation
and deallocation of these objects through constructors, destructors, 
insert and erase operations.

Hence it is possible:

struct Key;
struct Value;
typedef std::map<Key, Value> Map;

struct Key {};
struct Value {
    std::vector<Map::iterator> parents;
};

(All types and sizes are known)

Upvotes: 4

Paul Evans
Paul Evans

Reputation: 27567

You most likely want to store a smart pointer to the parent rather than an iterator.

Upvotes: 0

Related Questions