What is the difference between the back() and end() functions defined for list containers in STL?

It is specified in my book that back() gives reference to the last element while end() gives reference to the end of the list. I'm unable to understand how the two functions differ.

Recommended Answers

All 10 Replies

end( ) returns an iterator pointing to the last object. back( ) returns the last object( the value )

Okay. According to you, back() gives the value, not the reference. So, I'm understanding that this line

back() gives reference to the last element

given in the book is technically wrong. Thank you :)

Member Avatar for GreenDay2001

No, back() return reference.

By vmanes..

back( ) returns the last object( the value )

By vishesh..

No, back() return reference.

I'm confused. Both these members have said contradictory things. Someone please help me out.

For what it's worth this reference:

www.cppreference.com

indicates back() returns a reference to the last object. You can consult the standards to determine the answer if you wish.

It is my understanding that end() returns an iterator to one past the last object in the container, not an iterator to the last object in the container. This alllows for the following syntax:

list<int> myList;
list<int>::iterator start = myList.begin();
list<int>::iterator stop = myList.end();

for( ; start < stop; ++start)

rather than this:

for( ; start <= stop; ++start)

I've never figured out exactly what one past means. It may be implementation specific. It may mean the address of the bit/byte immediately beyond the address of the first bit/byte of memory used by the last item in the container, it may mean the first bit/byte of memory immediately after the block of memory used by the last item in the container, it may mean that there is a trailing node in the list/container that is automatically maintained by the list object, analagous to the this pointer. Who knows? It's only important if you're writing a class to extend/mimic the STL classes or are in discussions regarding changing the standards or are interested in the minutiae of the standards.

commented: Thank you for your time and interest :) +2

>Both these members have said contradictory things.
No, they said the same thing in a different way. back() returns a reference to the value of the last item in the collection. end() returns an iterator to one past the last item in the collection.

It may mean the address of the bit/byte immediately beyond the address of the first bit/byte of memory used by the last item in the container, it may mean the first bit/byte of memory immediately after the block of memory used by the last item in the container, it may mean that there is a trailing node in the list/container that is automatically maintained by the list object, analagous to the this pointer. Who knows? It's only important if you're writing a class to extend/mimic the STL classes or are in discussions regarding changing the standards or are interested in the minutiae of the standards.

I am in total agreement. My query is solved. Thank you :).

No, they said the same thing in a different way.

No. vmanes said something that was very much different. Have a look at this.

back( ) returns the last object( the value )

He said that back() returns the object. But, back() returns the reference and not the value. Now that the problem is solved, it does not matter anymore.

>No. vmanes said something that was very much different.
It's possible that he's confused and doesn't know the difference between a reference and a value, but I'm more inclined to believe that you simply misinterpreted his words.

Or I was just speaking in generalities.

Okay, agreed. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.