3

I've been working on learning c++, but now I'm stuck with a problem that really confuses me. The problem is that when i try to erase an element from a vector, the erase function does not erase the element that i wanted to be erased, but instead erases the last element from the vector. I recreated the problem with this piece of code, so that it's easier to understand my problem than it would be with my whole code:

#include <vector>
#include <iostream>

int main()
{
     std::vector<int> c;
     for(int i=0; i<=10; i++){
         c.push_back(i);
     }

    for (int i=0;i<c.size();i++) {
        std::cout << i << " ";
    }
    std::cout << '\n';

    c.erase(c.begin()+2);

    for (int i=0;i<c.size();i++) {
        std::cout << i << " ";
    }
    std::cout << '\n';

    c.erase(c.begin()+2, c.begin()+5);

    for (int i=0;i<c.size();i++) {
        std::cout << i << " ";
   }
    std::cout << '\n';

}

the result is not what is expected:

0 1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6

when I thought the result would be

0 1 2 3 4 5 6 7 8 9 10
0 1 3 4 5 6 7 8 9 10
0 1 2 7 8 9 10

Am I doing something completely wrong, or why is this not working as I thought it would? If it is relevant, I'm using the MinGW compiler for windows.

Tuotau
  • 41
  • 3

3 Answers3

9

After deletion, you do not print the content of the vector but only the loop variable.

Simply replace your cout sections by

for (int i=0;i<c.size();i++) {
    std::cout << c[i] << " ";
   }

and it will work as desired.

davidhigh
  • 14,652
  • 2
  • 44
  • 75
  • Hmm, I didn't make the same mistake in my actual code.. Should I edit the question to include the problematic part form my actual code, or ask again? Sorry for making this complicated.. – Tuotau Oct 19 '14 at 20:54
  • Regarding your question, I only know [this compilation](http://meta.codereview.stackexchange.com/questions/1763/for-an-iterative-review-is-it-okay-to-edit-my-own-question-to-include-revised-c/1765#1765) which sounds reasonable to me. Following it, you should ask a new question. – davidhigh Oct 19 '14 at 20:57
  • @Tuotau always post your actual code. [See here](http://stackoverflow.com/help/mcve) for code posting guidelines – M.M Oct 19 '14 at 20:57
  • @MattMcNabb I tried to follow the Minimal guideline, but actually made another mistake, so that I didn't actually manage to recreate the problem I initially had. But my bad for not being careful enough. – Tuotau Oct 19 '14 at 21:06
  • 1
    @Tuotau try again without the mistake :) (BTW I'm guessing that in the "real code" you actually use the same iterator before and after an erase operation?) – M.M Oct 19 '14 at 21:09
1

You print the loop variable, instead of the contents of the vector. For all instances:

Change

for (int i=0;i<c.size();i++) {
    std::cout << i << " ";
}

to

for (int i=0;i<c.size();i++) {
    std::cout << c.at(i) << " ";
}
tillaert
  • 1,797
  • 12
  • 20
0
for (int i=0;i<c.size();i++) {
    std::cout << i << " ";
}

should be:

for (int i=0;i<c.size();i++) {
    std::cout << c[i] << " ";
}

EDIT: I was late.

Reborn
  • 135
  • 2
  • 10