mercredi 25 juin 2014

Can you find the iterator for a list element given a pointer to the element?


Vote count:

0




The question is probably pretty self-explanatory, but If I have an std::list<T> object, is there any standard method to get the iterator for an element, given a T* value which points to the element?


If T is a user-defined type, it seems like dereferencing the pointer and using find_if would do the job, but this seems inefficient to me. If you're trying to find an element in a container by value, it makes sense that you'd need to iterate through the container until you find something. But if you have a pointer, it intuitively feels like there should be a method that's more direct. My basic understanding makes me think that there should be some 1-to-1 relationship between a list item's iterator and its pointer since lists are doubly-linked in the STL, but I really don't have much to back this up.


I'm not very familiar with with C++ iterators, so if anyone could explain why there is or isn't a way to do this, that would be helpful.



asked 43 secs ago

Matt

19





Aucun commentaire:

Enregistrer un commentaire