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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:40:02+00:00 2026-06-18T11:40:02+00:00

A bit of an elementary question, I’m looking for an STL container suited to

  • 0

A bit of an elementary question, I’m looking for an STL container suited to the following types of uses:

1) Supports regular forward iteration

2) Supports deletion from middle of list while walking backwards through the list, and

3) If something gets deleted while walking backwards, I need to stop and walk forwards to the end of the list.

I used a regular std::list and found walking forward from a point in the list to be a little difficult, but doable. I used a combination of forward and reverse_iterator, and managed to avoid using advance (because that would be expensive!)

Here is my code.

#include <list>
using namespace std ;

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

  // 1 2 3 4 5
  // should print:
  // 5: NOTHING
  // 4: 5
  // 3: 4,5
  // 2: 3,4,5
  // 1: 2,3,4,5
  for( list<int>::reverse_iterator riter = os.rbegin() ; riter != os.rend() ; ++riter )
  {
    //printf( "All the ones in FRONT of %d are:\n", riter->a ) ;
    printf( "%d:  ", *riter ) ;

    // You can't do it with a for loop.
    //for( list<O>::reverse_iterator iter = riter ; iter != os.rbegin() ; --iter )

    list<int>::reverse_iterator iter = riter ;

    if( iter != os.rbegin() ) do
    {
      --iter ; // move the iterator back.
      printf( " %d", *iter ) ;
    } while ( iter != os.rbegin() ) ;
    //else printf( " NOTHING AFTER ME" ) ;

    puts("");
  }
}

My questions are:

  • Have I made a bad choice of container? Should I have used deque instead?
  • Was there a way to do it using for instead of do/while in the inner loop?
  • 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-18T11:40:03+00:00Added an answer on June 18, 2026 at 11:40 am

    Considering your requirements, I believe your choice of list is good (but it’s std::list, not stl::list).

    A removal from the middle of a vector would invalidate all pointers, references, and iterators to elements previous to the removed one; a removal from the middle of a deque would invalidate all pointers, references, and iterators.

    Removal from a list, on the other hand, is guaranteed to invalidate only iterators to the element(s) you remove, so if you take proper care when removing an element you can keep iterating forward and backward.

    Considering your loop, I can’t help pointing out that:

    if (C) do { ... } while (C)
    

    Is equivalent to the more elegant (IMO):

    while (C) do { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Bit of an unusual question, but I have setup a field inside a MySQL
Bit of a javascript newbie so not sure if this question is easy or
Bit of a strange question here i know. but i wanted to know if
Bit support question. Apologies for that. I have an application linked with GNU readline.
Bit of a philosophical question. If you have a public website with a site
Bit of an odd question but is there a way of seeing what objects
Bit of a bizarre question, but does anyone know the actual limit to the
Bit of an odd question but.. I am currently building an app, it will
Bit of a general question here. Just wondering if there is a clever of
Bit of a newbie question as Im getting started with nHibernate. What's the difference

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.