Find the array by element

Hi,
Suppose I have some array.
1
2
3
  vector<obj*> vobj1;
  vector<obj*> vobj2;
  ......some more....

Now, I have a pointer of an element, but I don't know which vector it belongs to. Is there anyway to find the vector by that pointer?

Thanks.
Use a smart pointer (shared_ptr).
Smart pointer will not make one wiser in this case.

The question is similar to: I have many boxes. I might have put my pink bowtie into one of them. Have I?

The solution is to look at each box whether there is a pink bowtie. std::find
I thought that. But you'll have to create a vector of vector of obj*, iterate through them, check if the have the obj* with std::find and return a reference. Does this look good?
Last edited on
closed account (j3Rz8vqX)
I have a pointer of an element, but I don't know which vector it belongs to. Is there anyway to find the vector by that pointer?
Brute Force: Compare the address of the element that you have against every element within every vector; not the pointer's address, but the address of the object that is being pointed to.
seems like iteration is the only way to find it. Thanks for reply.
Pehaps a vector is not the most appropriate container type.

If the objects have a unique key, a std::map might be more suited to your problem.
You would still have multiple containers to search, but with a map, you can efficiently search for an object in each container.

Registered users can post here. Sign in or register to post.