473,659 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quick question: Getting an iterator to the last element

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
element in the container?

Or is there a cleaner way of getting an iterator to the last
element?
Jun 17 '07 #1
16 6069
Hi!

No, the STL does not guarantees that.
If you call operator-- on an empty container, you get some kind
of junk address (which will probably cause a SIGSEGV).
It is completely upon you to check for the existence of elements
in the container... but if there is any, operator-- will do its
job well.

Remember, if you have an empty data container, the last iterator
is the same as the first (on the GNU STL you have both them set
to 0, if I remember well).
You might so want to check for this condition: x.begin() != x.end()
or x.size() 0 ...

[If you do prefer for some reason accessing the last or the first
element directly (without passing through an iterator) you might
also use front() and back() on some containers (eg: vector).
They return trash if the container is empty anyway].

Bye!
Claudio A. Andreoni
Juha Nieminen wrote:
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
element in the container?

Or is there a cleaner way of getting an iterator to the last
element?
Jun 17 '07 #2
Juha Nieminen wrote:
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
element in the container?

Or is there a cleaner way of getting an iterator to the last
element?
rbegin(), assuming the container supports reverse iterators.

--
Ian Collins.
Jun 17 '07 #3
Claudio A. Andreoni wrote:
If you call operator-- on an empty container
Notice that I said this:
Juha Nieminen wrote:
>if there's at least one element in the data container
Jun 17 '07 #4
Ian Collins wrote:
rbegin(), assuming the container supports reverse iterators.
But if I need an iterator, not a reverse iterator?

std::list<int>: :iterator iter = l.rbegin();

error: conversion from
`std::reverse_i terator<std::_L ist_iterator<in t, int&, int*'
to non-scalar type
`std::_List_ite rator<int, int&, int*>' requested
Jun 17 '07 #5
On 2007-06-17 13:19, Juha Nieminen wrote:
Ian Collins wrote:
>rbegin(), assuming the container supports reverse iterators.

But if I need an iterator, not a reverse iterator?

std::list<int>: :iterator iter = l.rbegin();

error: conversion from
`std::reverse_i terator<std::_L ist_iterator<in t, int&, int*'
to non-scalar type
`std::_List_ite rator<int, int&, int*>' requested
You can use the base() method to get a normal iterator from a reverse
iterator, notice however that if you can base() on l.rbegin() you get
r.end() so you either have to increment the reverse iterator before
calling base(), or decrement the iterator returned by base() to get an
iterator to the last element.

All of this is quite a lot of extra work however, since --l.end() will
give you an iterator to the last element as long as l.size != 0.

Another thing to notice is that all of this (both using --l.end() and
the reverse-iterator) requires that the container supports bidirectional
iterators (though I'm not aware of any containers that don't).

--
Erik Wikström
Jun 17 '07 #6
In article <46************ **********@news .song.fi>,
no****@thanks.i nvalid says...
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
element in the container?

Or is there a cleaner way of getting an iterator to the last
element?
container.rbegi n() would be one obvious possibility.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 17 '07 #7
On Jun 17, 11:01 am, Juha Nieminen <nos...@thanks. invalidwrote:
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
element in the container?
No. Depending on how the iterator is implemented, it may or may
not be legal. (Obviously, it's never legal if the iterator
isn't at least bi-directional.)
Or is there a cleaner way of getting an iterator to the last
element?
The only sure way is:

C::iterator it = container.end() ;
-- it ;

For containers with random access iterators, container.end() - 1
also works.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 17 '07 #8
James Kanze wrote:
The only sure way is:

C::iterator it = container.end() ;
-- it ;
Isn't that exactly what I suggested in my question?
Jun 18 '07 #9
On 18 Jun, 11:08, Juha Nieminen <nos...@thanks. invalidwrote:
James Kanze wrote:
The only sure way is:
C::iterator it = container.end() ;
-- it ;

Isn't that exactly what I suggested in my question?
No. You suggested --container.end() which modifies a temporary - which
is not always legal. The usual example I've seen where your code won't
work is if the iterator is simply a raw pointer.

Gavin Deane

Jun 18 '07 #10

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

Similar topics

9
3498
by: kazio | last post by:
Hello, So, I need to have double linked, circular list (last element point to the first one and first one points to the last one). I thought maybe I could use list container from STL, but unfortunately probably not. I checked it, and when I increase (++) iterator pointing to the last element the program crashes. I know the standard says, this is a linear list (with beginning and the end), but I completely don't understand why they...
4
1883
by: matthurne | last post by:
I am working through exercise 8-2 in Accelerated C++...I am implementing the function equal(b, e, b2) where b is an iterator for the first element in a container, e is an iterator pointing to one past the last element in that same container, and b2 is an iterator for the first element in the second container. Here's what I have: template <class T> bool equal(T begin, T end, T begin2) { while (begin != end) { if (*begin != *begin2) {
0
8330
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.