473,326 Members | 2,061 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,326 software developers and data experts.

Iterator to last element in list?

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

Sep 22 '07 #1
12 9417
Howard wrote:
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.
How about list.end()-1 or list.start()+(list.size()-1)

--
Ian Collins.
Sep 22 '07 #2
Ian Collins wrote:
Howard wrote:
>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.
How about list.end()-1 or list.start()+(list.size()-1)
list iterators are bidirectional, not random access, so you can't do
arithmetic like this. Not to mention that list.size() is O(n).

But you can do:

list<T>::iterator it = l.end();
if( !l.empty())
--it;

Now "it" points to either the last element or, if the list is empty, end().
Sep 22 '07 #3
Mark P wrote:
Ian Collins wrote:
>Howard wrote:
>>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.
How about list.end()-1 or list.start()+(list.size()-1)

list iterators are bidirectional, not random access, so you can't do
arithmetic like this. Not to mention that list.size() is O(n).
Oops, sorry.

--
Ian Collins.
Sep 22 '07 #4

"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fmwrote in message
news:c%*******************@newssvr29.news.prodigy. net...
Ian Collins wrote:
>Howard wrote:
>>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.
How about list.end()-1 or list.start()+(list.size()-1)

list iterators are bidirectional, not random access, so you can't do
arithmetic like this. Not to mention that list.size() is O(n).

But you can do:

list<T>::iterator it = l.end();
if( !l.empty())
--it;

Now "it" points to either the last element or, if the list is empty,
end().
Yeah, that's what I'm doing now, but I was hoping for a single expression I
could use. This is working fine.

Thanks,
-Howard

Sep 22 '07 #5
In article <cL******************************@comcast.com>,
"Howard" <me@here.comwrote:
"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fmwrote in message
news:c%*******************@newssvr29.news.prodigy. net...
Ian Collins wrote:
Howard wrote:
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.

How about list.end()-1 or list.start()+(list.size()-1)
list iterators are bidirectional, not random access, so you can't do
arithmetic like this. Not to mention that list.size() is O(n).

But you can do:

list<T>::iterator it = l.end();
if( !l.empty())
--it;

Now "it" points to either the last element or, if the list is empty,
end().

Yeah, that's what I'm doing now, but I was hoping for a single expression I
could use. This is working fine.
assert( !l.empty() );
list<T>::iterator it = --l.end();
Sep 22 '07 #6
In article <cL******************************@comcast.com>,
"Howard" <me@here.comwrote:
"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fmwrote in message
news:c%*******************@newssvr29.news.prodigy. net...
Ian Collins wrote:
Howard wrote:
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.

How about list.end()-1 or list.start()+(list.size()-1)
list iterators are bidirectional, not random access, so you can't do
arithmetic like this. Not to mention that list.size() is O(n).

But you can do:

list<T>::iterator it = l.end();
if( !l.empty())
--it;

Now "it" points to either the last element or, if the list is empty,
end().

Yeah, that's what I'm doing now, but I was hoping for a single expression I
could use. This is working fine.
Or how about:

assert( !l.empty() );
list<T>::iterator it = l.rend().base();
Sep 22 '07 #7
Daniel T. wrote:
In article <cL******************************@comcast.com>,
"Howard" <me@here.comwrote:
>"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fmwrote in message
news:c%*******************@newssvr29.news.prodigy .net...
>>Ian Collins wrote:
Howard wrote:
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.
>
How about list.end()-1 or list.start()+(list.size()-1)

list iterators are bidirectional, not random access, so you can't do
arithmetic like this. Not to mention that list.size() is O(n).

But you can do:

list<T>::iterator it = l.end();
if( !l.empty())
--it;

Now "it" points to either the last element or, if the list is empty,
end().
Yeah, that's what I'm doing now, but I was hoping for a single expression I
could use. This is working fine.

Or how about:

assert( !l.empty() );
list<T>::iterator it = l.rend().base();
Isn't rend().base() the beginning of the list?
Sep 22 '07 #8
In article <RJ*****************@newssvr17.news.prodigy.net> ,
Mark P <us****@fall2005REMOVE.fastmailCAPS.fmwrote:
Daniel T. wrote:
In article <cL******************************@comcast.com>,
"Howard" <me@here.comwrote:
"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fmwrote in message
news:c%*******************@newssvr29.news.prodigy. net...
Ian Collins wrote:
Howard wrote:
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.

How about list.end()-1 or list.start()+(list.size()-1)

list iterators are bidirectional, not random access, so you can't do
arithmetic like this. Not to mention that list.size() is O(n).

But you can do:

list<T>::iterator it = l.end();
if( !l.empty())
--it;

Now "it" points to either the last element or, if the list is empty,
end().
Yeah, that's what I'm doing now, but I was hoping for a single expression
I
could use. This is working fine.
Or how about:

assert( !l.empty() );
list<T>::iterator it = l.rend().base();

Isn't rend().base() the beginning of the list?
Right, sorry. rbegin().base().
Sep 22 '07 #9
Daniel T. wrote:
In article <cL******************************@comcast.com>,
"Howard" <me@here.comwrote:
>"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fmwrote in message
news:c%*******************@newssvr29.news.prodigy .net...
Ian Collins wrote:
Howard wrote:
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.

How about list.end()-1 or list.start()+(list.size()-1)
list iterators are bidirectional, not random access, so you can't do
arithmetic like this. Not to mention that list.size() is O(n).

But you can do:

list<T>::iterator it = l.end();
if( !l.empty())
--it;

Now "it" points to either the last element or, if the list is empty,
end().

Yeah, that's what I'm doing now, but I was hoping for a single expression
I
could use. This is working fine.

assert( !l.empty() );
list<T>::iterator it = --l.end();
I prefer your other idea:

list<T>::iterator it = l.rbegin().base();

This is more generic, e.g. you can use it in algorithms templated on
containers:

template < typename Container >
typename Container::iterator
last_iter ( Container & c ) {
return ( c.rbegin().base() );
}

will always work whereas

template < typename Container >
typename Container::iterator
last_iter ( Container & c ) {
return ( -- c.end() );
}

can fail for std::vector where the iterator type might be a naked pointer.

Best

Kai-Uwe Bux
Sep 22 '07 #10
On 2007-09-21 21:21:08 -0400, "Daniel T." <da******@earthlink.netsaid:
>
assert( !l.empty() );
list<T>::iterator it = --l.end();
Maybe, but it's not required to work. l.end() returns a temporary, and
-- isn't required to be a member function. It usually is, so this will
work with most implementations.

list<T>::iterator it = l.end();
--it;

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 22 '07 #11
Kai-Uwe Bux wrote:
list<T>::iterator it = l.rbegin().base();
This won't work; rbegin().base() is equivalent to end().

-- Ben
Sep 22 '07 #12
Ben Rudiak-Gould wrote:
Kai-Uwe Bux wrote:
> list<T>::iterator it = l.rbegin().base();

This won't work; rbegin().base() is equivalent to end().
Ah, right.
Thanks

Kai-Uwe Bux

Sep 22 '07 #13

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

Similar topics

29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
7
by: andreas | last post by:
Hello, I have a problem with iterators in a fairly complex polygonal mesh data structure which is implemented using lists of geometric entities. However, the problem in itself is fairly simple:...
5
by: Mark Stijnman | last post by:
I have a question about forward iterators and what one should do or not do with them. I'm planning on writing a container that, when all boils down to it, stores a sequence of strings. I want...
16
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid...
15
by: Boltar | last post by:
Hi I'm going through an STL list container using a reverse iterator but it seems the erase() method only accepts ordinary iterators. Is there a similar method that will accept reverse iterators...
16
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last...
1
by: David Bilsby | last post by:
All Apologies for cross posing this but I am not sure if this is a VC 8 STL bug or simply an invalid use of the iterator. I have a PCI card access class which basically abstracts a third party...
3
by: JurgenvonOerthel | last post by:
I want to replace one element in a list<stringby a list<stringand I need to obtain an iterator to the first element in the inserted list. In code: void...
5
by: remlostime | last post by:
struct nodeType { int v, index; }; list<nodeTypenode; for(list<nodeType>::iter = node].begin(); iter != node].end(); iter++) What's wrong with it? how can i fix it?
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.