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 8587535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:35:33+00:00 2026-06-11T22:35:33+00:00

Consider the toy program (post.cpp): #include <iostream> #include <vector> using namespace std; int main()

  • 0

Consider the toy program (post.cpp):

#include <iostream>
#include <vector>

using namespace std;
int main() {
        vector<int > a;
        int i;
        for(i=0;i<10;i++)
                a.push_back(i);
        auto it=a.rbegin();
        while(it!=a.rend()) {
                if ((*it % 2)==0) {
                                cout << "about to erase "<<*it<<endl;
                                a.erase((it++).base());
                }
                else {
                        ++it;
                }
        }
        for(auto it2=a.begin(); it2 != a.end(); it2++) {
                cout << *it2 << endl;
        }
        return 0;
}

What I am trying to do is to test for evenness, and then delete the current number, since (it++) should return the current iterator and then advance the iterator. This is what I get as the output:

$ ./post 
about to erase 8
about to erase 6
about to erase 4
about to erase 2
about to erase 0
0
2
4
6
8

If, however, I change the line a.erase((it++).base()); to a.erase((++it).base());, I get the correct behavior. Why is this?

Useful clarification: I am using base() since reverse_iterators cannot be used in erase(). There is an application where I want to go reverse on the vector to erase stuff.

  • 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-11T22:35:35+00:00Added an answer on June 11, 2026 at 10:35 pm

    The base() of a reverse iterator is offset by 1. So rbegin().base() == end() and rend().base() == begin().

    This is nothing other than the generalization of this reverse loop:

    for (size_t i = 0; i != N; ++i)
    {
        mutate(array[N - i - 1]);
    }   //                 ^^^
    

    The loop traverses the array in reverse order, and note how we need a - 1 on the “iterator”.


    Update: Now let’s investigate what a reverse iterator is: It is simply a wrapper around a bidirectional iterator. Suppose the reverse iterator is called it; then it has an ordinary iterator member it.base(). For example, v.rbegin().base() == v.end(). When you say ++it, that just calls --it.base() (conceptually). The real magic is the dereference operation: This has to give us one element before the underlying iterator:

    *it == *(it.base() - 1)
    

    This is exactly the same arithmetic which told us that the i th element from the back of an array is offset by one:array[N - i - 1]. This also shows us why we need a bidirectional iterator to form reverse iterators.

    Now it is clear how we can erase via reverse iterators from a container that does not invalidate iterators, such as any node-based container:

    if (meets_condition(*it))   // this examines *(it.base() - 1)!
    {
         auto b = it.base();
         container.erase(it.base() - 1);
         it = std::reverse_iterator(b);
    }
    

    Remember that this requires that erasing does not invalidate any iterators other than the erasee, like in any node-based container. Erasing like this from a vector would be even more difficult. For a vector, erasing invalidates all iterators past the erasee (in forward direction), so we have to use the return value of the erase function:

    if (meets_condition(*ut))   // again, examine *(it.base() - 1)
    {
        it = std::reverse_iterator(container.erase(it.base() - 1));
    }
    

    In a picture (we’re removing element “5”):

    +---+---+---+---+---+---+---+
    | 2 | 3 | 4 | 5 | 6 | 7 | 8 |     =:   v
    +---+---+---+---+---+---+---+
                  ^   ^
                  |   |
    |             |   +--- it.base()
    |             |
    |             +--- *it == *(it.base() - 1)
    |
    V
    
    +---+---+---+---+---+---+
    | 2 | 3 | 4 | 6 | 7 | 8 |
    +---+---+---+---+---+---+
              ^   ^
              |   |
              |   +--- result of v.erase(it.base() - 1)
              |
              +--- *(std::reverse_iterator(v.erase(it.base() - 1)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code: #include <iostream> using namespace std; typedef int array[12]; array sample; array
Consider having the following header file (c++): myclass.hpp #ifndef MYCLASSHPP_ #define MYCLASSHPP_ namespace A
Consider the following C#: // C# .net switch(x) { case 1: for(int i =
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following Mechanize form object #<Mechanize::Form {name f1} {method POST} {action f.php} {fields
Consider the following expressions in Java. int[] x={1, 2, 3, 4}; int[] y={5, 6,
Consider this code: int a = 5; if (a == 5 || a ==
Consider this in C#: int[] a = new int[1024]; All integers will be set
Consider the following (simplified) situation: class Foo { private: int evenA; int evenB; int
Consider: void f(std::pair<bool,bool> terms = std::pair<bool,bool>(1,1)) {} gcc 4.4 is ok, gcc 4.3 complains

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.