I just asked a question involving std::set but after thinking about it some more, I think I’ve narrow down my problem to something simple.
I have a set of unique items. I’d like to create a vector where each element in the vector points to an item in the set.
So, I have a set of (unique) A objects:
class A
{
int i;
};
std::set<A, compareclass> mySet;
and a vector of A*:
std::vector<A*> myVec;
set::insert() returns a pair which includes an iterator to the element (inserted or already existing) in the set. I could technically get it’s address like so:
ret = myset.insert(A());
myVec.push_back(&(*ret.first));
I’m thinking that might not be a great idea, though. Is there any other way to allow a vector element to point to a specific object in the set?
I'm thinking that might not be a great idea, why do you think that? The only thing I see you is that you could do is to use something along the lines of a smart pointer instead of a plain pointer to clarify ownership, and avoid memory leak mistakes that come along with them.Or like suggested by Jerry, iterators are even better!