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

  • Home
  • SEARCH
  • 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 100423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:34:36+00:00 2026-05-11T00:34:36+00:00

For some reason the following code fails. You can’t simply erase a reverse_iterator by

  • 0

For some reason the following code fails. You can’t simply erase a reverse_iterator by using its base() method.

#include <set> #include <iostream>  int main() {     std::set<int> setOfInts;     setOfInts.insert(1);     setOfInts.insert(2);     setOfInts.insert(3);      std::set<int>::reverse_iterator rev_iter = setOfInts.rbegin();     std::set<int>::reverse_iterator nextRevIter = setOfInts.rbegin();     ++nextIter;      while ( rev_iter != setOfInts.rend())     {         // Find 3 and try to erase         if (*rev_iter == 3)         {             // SEGFAULT HERE             setOfInts.erase( rev_iter.base());         }         rev_iter = nextRevIter;         ++nextRevIter;     }  } 

How does one go about correctly doing the above? Given a reverse_iterator that corresponds to something you want to erase, how do you erase it?

Note, erase won’t take reverse_iterators unfortunately. It wants the real thing.

  • 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. 2026-05-11T00:34:36+00:00Added an answer on May 11, 2026 at 12:34 am

    Apparently the solution is what base() returns is 1 off. The following identity holds for a reverse_iterator:

    &*(reverse_iterator(i)) == &*(i - 1)  

    Or in other words, the reverse_iterator is always one pass the regular iterator it is the base of. Not sure why.

    In GCC

    Simply change

            // SEGFAULT HERE         setOfInts.erase( rev_iter.base()); 

    to

            // WORKS!         setOfInts.erase( --rev_iter.base()); 

    I’m definitely curious though as to why the identity above makes sense.

    In Visual Studio

    Coming back into work and trying this in visual studio, I see the above solution doesn’t quite work. The ‘nextIter’ becomes invalid on the erase. Instead, you need to save away the temporary from the erase to get the next iterator instead of keeping around a nextIter like above.

      set<int>::iterator tempIter = setOfInts.erase(--rev_iter.base());   rev_iter = setOfInts.erase(tempIter); 

    So the final solution is

    int main() {     using namespace std;      set<int> setOfInts;     setOfInts.insert(1);     setOfInts.insert(2);     setOfInts.insert(3);      set<int>::reverse_iterator rev_iter = setOfInts.rbegin();      while ( rev_iter != setOfInts.rend())     {         // Find 3 and try to erase         if (*rev_iter == 3)         {             cout << 'Erasing : ' << *rev_iter;             set<int>::iterator tempIter = setOfInts.erase( --rev_iter.base());             rev_iter = set<int>::reverse_iterator(tempIter);                     }         else         {             ++rev_iter;         }     }     } 

    Note, associative containers do not return an iterator from erase. So this solution wouldn’t work for map, multimap, etc.

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

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer If you're lucky enough to have a choice, release as… May 11, 2026 at 10:02 am
  • added an answer The simplest protocols often use a separate connection for every… May 11, 2026 at 10:02 am
  • added an answer AFAIK there is no direct method or property to check… May 11, 2026 at 10:02 am

Related Questions

For some reason the following code fails. You can't simply erase a reverse_iterator by
The following html code works in Firefox, but for some reason fails in IE
Please consider the following: <td style=width: 500px;> <div style=width: 400px;>SomeContent</div> </td> For some reason,
What is the reason for the following warning in some C++ compilers? No newline
For some reason the Windows command prompt is special in that you have to
For some reason the system admin changed my user name from XxXx to XxXx1
For some reason beyond me I can't access the mysql server on a machine.
for some reason I cannot see any of the generated objects under the TL
For some reason, the event listener I define never seems to receive any events,
For some reason, lately the *.UDL files on many of my client systems are

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.