Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9287987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:48:43+00:00 2026-06-18T19:48:43+00:00

Tested on Mac OS X using XCode 4.6. This example code shows removing the

  • 0

Tested on Mac OS X using XCode 4.6.

This example code shows removing the last element of an std::list works as I expected: an iterator reference to list::end() is still “1 past the end” and is still valid, even through removal of the last element.

But the second example counters my intuition. Removing the first element of the list changes list::rend(), which I thought was “1 past the beginning”.

Was my expectation wrong? Why was it wrong? Why does your reference to “1 past the end” through deletion of the last element remain valid (should it not?), but a reference to “1 in front of the beginning (.rend()) becomes invalid after removal of the front element?

void printList( list<int>& os )
{
  for( int& i : os )
    printf( "%d ", i ) ;
  puts("");
}

void testList()
{
  list< int > os ;
  os.push_back( 1 ) ;
  os.push_back( 2 ) ;
  os.push_back( 3 ) ;
  os.push_back( 4 ) ;
  os.push_back( 5 ) ;  

  // Forward iterators:  reference to .end() not invalidated when remove last elt.
  list<int>::iterator fwdEnd = os.end() ;
  printList( os ) ;
  os.erase( --os.end() ) ; // remove the 5 (last elt)
  printList( os ) ;
  if( fwdEnd == os.end() )  puts( "YES, fwdEnd==os.end() still, iterators not invalidated" ) ;  // I get __this__ result
  else puts( "NO: fwdEnd INVALIDATED" ) ;



  list<int>::reverse_iterator revEnd = os.rend() ;
  // remove the front element
  printList( os ) ;
  os.erase( os.begin() ) ; // removes the 1
  printList( os ) ;
  if( revEnd == os.rend() )  puts( "YES revEnd is still valid" ) ;
  else  puts( "NO: revEnd NOT valid" ) ; // I get __this__ result
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T19:48:44+00:00Added an answer on June 18, 2026 at 7:48 pm

    This is due to the fact that a reverse iterator has a slightly different referencing logic than a regular iterator: it points to an element, but when dereferenced, it yields a reference to the previous element.

    You will easily see this if you try the following:

    #include <vector>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        vector<int> v = { 1, 2, 3, 4, 5, 6 };
        auto i = find(begin(v), end(v), 3);
        cout << *i << endl;
    
        vector<int>::const_reverse_iterator ri(i);
        cout << *ri << endl;
    }
    

    The output should be:

    3
    2
    

    When a reverse iterator physically points to a certain element, it logically points to the element which precedes it. Thus, a reverse iterator physically pointing to the element in a collection with index i, when dereferenced, yields (a reference to) the element with index i-1:

                           i, *i
                           |
        -      1     2     3     4     5     6     -
                     |     | 
                     *ri   ri
    

    This is the reason why an iterator return by rend() actually points to the first element in a collection, and not to the one before the first element. Removing the first element, therefore, invalidates it.

               begin, *begin                       end, *end
               |                                   |
        -      1     2     3     4     5     6     -
        |      |                             |     |
    *rend      rend                    *rbegin     rbegin
    

    This does not apply only to lists, but to all collections which offer bidirectional iterators.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running some code on Mac OSX 10.6.6 and XCode 3.2.4 and I have
Here is my code: jsFiddle . I've tested this in the latest versions of
Tested out the same code in Visual Studio on Windows to make sure. Using
Tested app in Instrumens for memory leak getting multiple leaks for using multiple times
I tested the following code in firefox scratchpad and got interesting result? var date=new
I tested out the script below in jsfiddle and it works fine, can someone
I tested this on both .Net 4.0 and .Net 4.0 CP, same result. This
I tested each statement on its own and it works, but when I use
I've tested this snippet with 3 different TTF fonts and I still din't get
I'm using resque/redis on heroku to send emails as a background job. It works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.