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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:59:14+00:00 2026-05-17T23:59:14+00:00

Dream output: /* DREAM OUTPUT: INT: 1 TESTINT: 1 TESTINT: 2 TESTINT: 23 TESTINT:

  • 0

Dream output:

 /* DREAM OUTPUT:
 INT: 1
 TESTINT: 1
 TESTINT: 2
 TESTINT: 23
 TESTINT: 24
 TESTINT: 25
 TESTINT: 3
 TESTINT: 4
 TESTINT: 5
 TESTINT: 6
 INT: 23
 INT: 24
 INT: 25
 INT: 3
 INT: 4
 INT: 5
 INT: 6

Problem

 ERROR 1: Not erasing the '2' causes a bizzare effect.
 ERROR 2: Erasing the '2' causes memory corruption.

Code

 #include <cstdlib>
 #include <iostream>
 #include <vector>

 int main(int argc, char* argv[])
 {
    std::vector<int> someInts;

    someInts.push_back(1);
    someInts.push_back(2);
    someInts.push_back(3);
    someInts.push_back(4);
    someInts.push_back(5);
    someInts.push_back(6);

    for(std::vector<int>::iterator currentInt = someInts.begin();
        currentInt != someInts.end(); ++currentInt)
     if(*currentInt == 2)
     {
        std::vector<int> someNewInts;

        someNewInts.push_back(23);
        someNewInts.push_back(24);
        someNewInts.push_back(25);

        someInts.insert(currentInt + 1, someNewInts.begin(), someNewInts.end());
        //someInts.erase(currentInt);

        for(std::vector<int>::iterator testInt = someInts.begin();
            testInt != someInts.end(); ++testInt)
         std::cout << "TESTINT: " << *testInt << '\n';
     }
     else
        std::cout << "INT: " << *currentInt << '\n';

    return 0;
 }

The code is pretty self-explanatory, but I’d like to know what’s going on here. This is a replica using ints of what’s happening in a much larger project. It baffles me.

  • 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-05-17T23:59:15+00:00Added an answer on May 17, 2026 at 11:59 pm

    You need to understand the differences between the stl collections.

    A Vector is a continuous (usually) block of memory. Whem you insert into the middle, it tries to be helpful by re-allocating enough memory for the existing data plus the new, then copying it all to the right places and letting you continue as if nothing had happened. However, as you’re finding – something has happened. Your iterators that used to refer to the old memory block, are still pointing there – but the data has been moved. You get memory errors if you try to use them.

    One answer is to determine where the iterator used to point, and update it to point to the new location. Typically, people use the [] operator for this, but you can use begin() + x (where x is the index into the vector).

    Alternatively, use a collection whose iterators are not invalidated by inserting. The best one for this is the list. Lists are constructed from little blocks of memory (1 per item) with a pointer to the next block along. This makes insertion very quick and easy as no memory needs to be modified, just the pointers to the blocks either side of the new item. Your iterator will still be valid too!

    Erasing is just the same, except once you delete the item your iterator refers to, its invalid (obviously) so you cannot make any operation on it. Even ++ operator will not work as the memory might have changed in a vector, or the list pointers be different. So, you can first get an iterator to the next element, store it and then use that once you’ve deleted an item, or use the return value from the erase() method.

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

Sidebar

Related Questions

My dream IDE does full code hints, explains and completes PHP, Javascript, HTML and
I am currently trying out this code: NSString *path = [[NSBundle mainBundle] pathForResource:@dream ofType:@m4a];
Either I had a bad dream recently or I am just too stupid to
I dream of creating a control which works something like this: <asp:SqlDataSource id=dsFoo runat=server
I just noticed, using the HTC Dream that using a bluetooth headset was really
Hallo, first of all, i am not very familiar when it comes to xml
This question is purely academic, because I'd never dream of doing this in real
a question that I hope does not sound crazy: On a multilingual site, imagine
I'm building a CMS in PHP and one dread I have is that the
Some time ago I saw a XML library for C++ which heavily utilized operator

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.