By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Quick question: Getting an iterator to the last element

P: n/a
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
Share this Question
Share on Google+
16 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.