last element of list

X

xerix

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. 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?
 
A

Arne Adams

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. 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?
try std::list<T>::back() and make sure that your list is not empty

Arne
 
X

xerix

Arne Adams said:
is
try std::list<T>::back() and make sure that your list is not empty

Arne

back returns a reference to the last element from the list, but I need an
iterator to the last element of the list.
 
S

Sander

xerix said:
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. 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?
I don't know much about C++, but doesn't give end() an iterator to the
position where the next object will come? If so, you could first call end()
and then add the new object.

Sander
 
X

xerix

Sander said:
is
I don't know much about C++, but doesn't give end() an iterator to the
position where the next object will come? If so, you could first call end()
and then add the new object.

Sander

That ain't working either :(
 
N

Nick Hounsome

xerix said:
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. 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?

list has bidirectional iterators so you can do end()-1
 
J

John Harrison

xerix said:
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.

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
 
X

xerix

Nick Hounsome said:
is

list has bidirectional iterators so you can do end()-1

error C2784: 'ptrdiff_t std::eek:perator -(const std::reverse_iterator<_RanIt>
&,const std::reverse_iterator<_RanIt> &)' : could not deduce template
argument for 'const std::reverse_iterator<_RanIt> &' from
'std::list<_Ty,_Ax>::iterator'
with
[
_Ty=int,
_Ax=std::allocator<int>
]

iter = listNum.end()--;

This isn't working either.
I'm using reverse iterators as a workaround, but that isn't actually what I
want.
 
X

xerix

John Harrison said:
The list<T>::insert function already returns an iterator to the newly
inserted element.

Aarg! This is what I was looking for. I just completely missed the insert
function because I always use push_back. I feel so stûpid.
Thank you!
 
K

Kevin Goodsell

Nick said:
list has bidirectional iterators so you can do end()-1

You can't do that with bidirectional iterators. You might be able to do
--my_list.end(), though. It doesn't necessarily work for vectors and
strings, but should work for lists.

-Kevin
 
N

Nick Hounsome

xerix said:
error C2784: 'ptrdiff_t std::eek:perator -(const
std::reverse_iterator said:
&,const std::reverse_iterator<_RanIt> &)' : could not deduce template
argument for 'const std::reverse_iterator<_RanIt> &' from
'std::list<_Ty,_Ax>::iterator'
with
[
_Ty=int,
_Ax=std::allocator<int>
]

iter = listNum.end()--;

This isn't working either.
I'm using reverse iterators as a workaround, but that isn't actually what I
want.

Why are you using reverse iterators?
If you want them then the last element is just list.rbegin()
You can then use base() to get a forward iterator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top