Quick way to get an iterator to the last element of a list

Started by
14 comments, last by Andrew Russell 18 years, 9 months ago
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?
Advertisement
std::list::back()

EDIT: SGI has a good reference page if you need it in the future.

EDIT2:
Quote:Original post by Red Ant
That would give me a reference to the last element in the list, but not an iterator pointing to that element.


Whoops. Misread the question [disturbed]

[Edited by - load_bitmap_file on June 22, 2005 7:53:47 PM]
That would give me a reference to the last element in the list, but not an iterator pointing to that element.
rbegin() should be an iterator pointing at the last element [iirc]. If all you want is the last element, who cares if it's a forward or reverse iterator? Calling list.erase(reverse_iterator) should still eliminate the object contained at the iterator's position.
Hmm, come to think it ... you're right! Why didn't I think of thst lol. Thanks!
Ah shoot, it doesn't work. The compiler complains about not being able to convert a reverse_iterator to an iterator when I pass the thing to std::list::erase().


P.S. I think I've found a solution. It doesn't really matter if I add an element at the back or the front of the list. So if just do push_front and then get a standard iterator with std::list::begin(), I should be fine.
Seeming you want do erase the last element of your list, isn't
StructList::pop_back()
an option?
No, because by the time I'm going to delete a particular instance from the list it is perfectly possible that it will no longer be the last (or the first, for that matter) element in the list.
It's ugly and maybe not portable (haven't checked), but... "--(list.end())"

#include <list>#include <iostream>int main() {        std::list<int> l;        for(int i = 0; i < 10; ++i) {                std::cout << "pushing " << i << "\n";                l.push_back(i);        }        std::list<int>::iterator i = --(l.end());        std::cout << "i: " << *i << std::endl;}



Hope this helps.
StructList::iterator itLastElem = (++m_theList.rbegin()).base();


Enigma

This topic is closed to new replies.

Advertisement