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 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.
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().
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.
"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
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();
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();
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?
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().
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
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)
Kai-Uwe Bux wrote:
list<T>::iterator it = l.rbegin().base();
This won't work; rbegin().base() is equivalent to end().
-- Ben
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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:...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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?
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
|
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...
| |