Hi y'all.
Okay, before I get massacred for asking stupid questions, I have run a forum search to see if this question has been asked before, but I honestly couldn't find anything that would have helped me with this. So here goes:
Basically I have a struct that I store in a list (STL). The struct looks like this
// Forward declaration.
struct MyStruct;
typedef std::list< MyStruct* > StructList;
struct MyStruct
{
/// Loads of terribly useful data ...
/// IMPORTANT: An iterator that I need to point to this instance's position in the list that stores MyStruct objects.
StructList::iterator m_itMyOwnPositInTheList;
};
After I have added a MyStruct instance to the list, I want to get an iterator to the exact position at which the instance has ended up and then set that instance's itMyOwnPositInTheList member to that. It is useful for my instances to know exactly where in the list they reside, so they can later be erased quickly without having to walk the list first.
So far so good. However, I'm having trouble actually obtaining this iterator ... or at least doing it without iterating the list right up to the last element (which is where I suspect push_back would place the new list element). And I can't do
StructList::iterator itLastElem = m_theList.end();
--itLastElem ;
right? Because once an iterator points to the end it's not really a valid iterator anymore (or is it?). I've also tried
StructList::iterator itLastElem = m_theList.rbegin();
++itLastElem;
But apparently you can't convert a reverse iterator to a normal one like that. It would be really good if there was a method of putting something in a list and then getting an iterator to that exact position. Any idess?