473,387 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

last element of list

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?
Jul 22 '05 #1
20 2242
"xerix" schrieb
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
Jul 22 '05 #2
"xerix" schrieb
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
Jul 22 '05 #3

"Arne Adams" <in***@arneadams.com> wrote in message
news:c5*************@news.t-online.com...
"xerix" schrieb
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


back returns a reference to the last element from the list, but I need an
iterator to the last element of the list.
Jul 22 '05 #4

"Arne Adams" <in***@arneadams.com> wrote in message
news:c5*************@news.t-online.com...
"xerix" schrieb
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


back returns a reference to the last element from the list, but I need an
iterator to the last element of the list.
Jul 22 '05 #5
"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. 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
Jul 22 '05 #6
"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. 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
Jul 22 '05 #7

"Sander" <sa*******@nospam.hotmail.com> wrote in message
news:40**********************@dreader2.news.tiscal i.nl...
"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. 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


That ain't working either :(
Jul 22 '05 #8

"Sander" <sa*******@nospam.hotmail.com> wrote in message
news:40**********************@dreader2.news.tiscal i.nl...
"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. 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


That ain't working either :(
Jul 22 '05 #9

"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. 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
Jul 22 '05 #10

"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. 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
Jul 22 '05 #11

"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
Jul 22 '05 #12

"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
Jul 22 '05 #13

"Nick Hounsome" <nh***@blueyonder.co.uk> wrote in message
news:Ej****************@news-binary.blueyonder.co.uk...

"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. 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


error C2784: 'ptrdiff_t std::operator -(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.
Jul 22 '05 #14

"Nick Hounsome" <nh***@blueyonder.co.uk> wrote in message
news:Ej****************@news-binary.blueyonder.co.uk...

"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. 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


error C2784: 'ptrdiff_t std::operator -(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.
Jul 22 '05 #15

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c5*************@ID-196037.news.uni-berlin.de...

"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.


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!
Jul 22 '05 #16

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c5*************@ID-196037.news.uni-berlin.de...

"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.


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!
Jul 22 '05 #17
Nick Hounsome wrote:
"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. 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


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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #18
Nick Hounsome wrote:
"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. 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


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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #19

"xerix" <xe***@bla.nl> wrote in message
news:b1***************************@news.multikabel .nl...

"Nick Hounsome" <nh***@blueyonder.co.uk> wrote in message
news:Ej****************@news-binary.blueyonder.co.uk...

"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. 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


error C2784: 'ptrdiff_t std::operator -(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.

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.

Jul 22 '05 #20

"xerix" <xe***@bla.nl> wrote in message
news:b1***************************@news.multikabel .nl...

"Nick Hounsome" <nh***@blueyonder.co.uk> wrote in message
news:Ej****************@news-binary.blueyonder.co.uk...

"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. 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


error C2784: 'ptrdiff_t std::operator -(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.

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.

Jul 22 '05 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: xerix | last post by:
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...
8
by: sudeep | last post by:
Data Storage Problem Statement In an organization there are 3 kinds of people: Staff, Students and Research Associates. Research Associates are actually students who are considered as staff,...
57
by: pinkfloydhomer | last post by:
Isn't there an easier way than lst = ... ?
12
by: Howard | last post by:
Is there an easy way to get an iterator (*not* a reverse-iterator) to the last element in a list? The last() function returns the element itself, not an iterator. Thanks, -Howard
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.