472,973 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 5980
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_iterator<std::_List_iterator<int, int&, int*'
to non-scalar type
`std::_List_iterator<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_iterator<std::_List_iterator<int, int&, int*'
to non-scalar type
`std::_List_iterator<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.invalid 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.rbegin() 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*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
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
Gavin Deane wrote:
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.
Why would your version make any difference?
Jun 19 '07 #11
Juha Nieminen wrote:
Gavin Deane wrote:
>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.

Why would your version make any difference?
Consider:

#include <cstddef>
#include <cassert>

template < typename T, std::size_t N >
class array {

T the_data [N];

public:

typedef T* iterator;

array ( T const & value ) {
for ( std::size_t i = 0; i < N; ++i ) {
the_data[i] = value;
}
}

T & operator[] ( std::size_t i ) {
assert( i < N );
return ( the_data[i] );
}

T const & operator[] ( std::size_t i ) const {
assert( i < N );
return ( the_data[i] );
}

std::size_t size ( void ) const {
return ( N );
}

iterator begin ( void ) {
return ( the_data );
}

iterator end ( void ) {
return ( the_data + N );
}

}; // array
typedef array< double, 3 point3d;

int main ( void ) {
point3d P (0.1);
{
// compile time error:
point3d::iterator before_end = -- P.end();
}
{
// fine:
point3d::iterator before_end = P.end();
-- before_end;
}
}
If you had an implementation of std::vector<that uses pointers to
implement iterators, you would run into similar problems.
Best

Kai-Uwe Bux
Jun 19 '07 #12
Kai-Uwe Bux a écrit :
Juha Nieminen wrote:
>Gavin Deane wrote:
>>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.
Why would your version make any difference?

[snip] code
If you had an implementation of std::vector<that uses pointers to
implement iterators, you would run into similar problems.
I agree with you, this is buggy behavior.

Just not to be misleading about iterator being pointer, such an
implementation would lack iterator traits.

A minimal Input iterator would be:
struct iterator
{
//traits
typedef T value_type;
typedef ptrdiff_t distance_type;
//etc

//actual pointer
T* pointer;
//constructor
iterator(T* def=NULL):pointer(def){}
//assignment operator
iterator operator=(const iterator& it)
{
this->pointer=it.pointer;
return *this;
}
//equality/inequality operator
bool operator==(const iterator& it){return this->pointer==it.pointer;}
bool operator!=(const iterator& it){return this->pointer!=it.pointer;}
//dereference operator
T operator*(){return *(this->pointer);}

//pre/post increment operator
iterator operator++(){++this->pointer;return *this;}
iterator operator++(int){iterator
old(this->pointer);++this->pointer;return old;}
iterator operator--(){--this->pointer;return *this;}
iterator operator--(int){iterator
old(this->pointer);--this->pointer;return old;}
};

And in this case, the example you gave do compile.
Michael.
Jun 19 '07 #13
Michael DOUBEZ wrote:
Kai-Uwe Bux a écrit :
>Juha Nieminen wrote:
>>Gavin Deane wrote:
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.
Why would your version make any difference?

[snip] code
>If you had an implementation of std::vector<that uses pointers to
implement iterators, you would run into similar problems.

I agree with you, this is buggy behavior.
Well, buggy in the sense that the user's code is incorrect. Pointers are
valid iterators.
Just not to be misleading about iterator being pointer, such an
implementation would lack iterator traits.
No, you get iterator traits from the iterator_traits<Itertemplate
instantiation, not directly from the iterator.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jun 19 '07 #14
Michael DOUBEZ wrote:
Kai-Uwe Bux a écrit :
>Juha Nieminen wrote:
>>Gavin Deane wrote:
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.
Why would your version make any difference?

[snip] code
>If you had an implementation of std::vector<that uses pointers to
implement iterators, you would run into similar problems.

I agree with you, this is buggy behavior.
Buggy as in "not standard compliant" or buggy as in "I don't like it"?

Just not to be misleading about iterator being pointer, such an
implementation would lack iterator traits.
[snip]

Huh? By [24.3.1/2], std::iterator_traits<is specialized for any pointer
type T* as:

template<class T>
struct iterator_traits<T*{
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef random_access_iterator_tag iterator_category;
};

and for T const * as:

template<class Tstruct iterator_traits<const T*{
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef const T* pointer;
typedef const T& reference;
typedef random_access_iterator_tag iterator_category;
};
I thought, a standard conforming implementation of std::vector<Tcould use
T* as iterator and T const * as const_iterator. What is it that I am
missing?
Best

Kai-Uwe Bux
Jun 19 '07 #15
Kai-Uwe Bux a écrit :
Michael DOUBEZ wrote:
>Kai-Uwe Bux a écrit :
>>Juha Nieminen wrote:

Gavin Deane wrote:
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.
Why would your version make any difference?
[snip] code
If you had an implementation of std::vector<that uses pointers to
implement iterators, you would run into similar problems.
I agree with you, this is buggy behavior.

Buggy as in "not standard compliant" or buggy as in "I don't like it"?

>Just not to be misleading about iterator being pointer, such an
implementation would lack iterator traits.
[snip]

Huh? By [24.3.1/2], std::iterator_traits<is specialized for any pointer
type T* as:

I thought, a standard conforming implementation of std::vector<Tcould use
T* as iterator and T const * as const_iterator. What is it that I am
missing?
Nothing. I missed the point.
Michael
Jun 19 '07 #16
On Jun 19, 11:02 am, Juha Nieminen <nos...@thanks.invalidwrote:
Gavin Deane wrote:
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.
Why would your version make any difference?
The variable it isn't a temporary.

The version I proposed is guaranteed to work with any
bi-directional iterator. The version you proposed isn't.
That's the difference.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 20 '07 #17

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

Similar topics

9
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...
4
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.