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 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 76k
  • Answers 77k
  • 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 Edit: Just reread the title of your problem. You want… May 11, 2026 at 3:16 pm
  • added an answer One of the simplest ways to ensure you get all… May 11, 2026 at 3:16 pm
  • added an answer It turns out this can be done. <Rectangle x:Name='myRect' Width='28'… May 11, 2026 at 3:16 pm

Related Questions

For some reason the following code fails. You can't simply erase a reverse_iterator by
I'm trying to launch another process from a service (it's a console app that
The following html code works in Firefox, but for some reason fails in IE
The code goes something like this: protected bool IsOKToSend() { bool IsOK = true;
I've got the following bit of code (that another developer wrote) that I need

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.