"xerix" <xe***@bla.nl> wrote in message
news:7f***************************@news.multikabel .nl...
I'm trying to make a function which insert an object into a list and
returns an iterator to the new element in the list.
The list<T>::insert function already returns an iterator to the newly
inserted element.
But as far as I know there is no
function which returns an iterator to the last element of a list. (.end()
returns an iterator one item past the end of the list) How could I solve
this?
Well I don't really see the connection between your two sentences, but if
you really want a function that returns an iterator to the last element of a
list then all you have to do is write one. Just use end() to get the one
past the end and then operator-- to move back to the end of the last.
template <class T>
list<T>::iterator last_element(list<T>& lst)
{
list<T>::iterator i = lst.end();
return --i;
}
Of course bad things will happen if you use this function on an empty list.
john