If the loop finds a repeated number, i want to delete that number. Im not sure what im doing wrong (i am assuming all numbers are sorted). It should delete the last '3' but im getting errors about variable 'x' and how it is uninitalized.
Im trying to make a more general formula and the problem i have now is that the vector subscript is out of range. Im pretty sure this is because of the number[x+1] but im not sure what to use in order to compare number[x] to what is directly after it.
this is what im stuck with.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
unsignedint i = 0;
vector<int>number;
number.push_back(1);
number.push_back(2);
number.push_back(3);
number.push_back(3);
number.push_back(4);
for (unsignedint x = 0; x < number.size(); x++) {
if (number[x] == number[x+1]) {
number.erase(number.begin() + x);
}
}
I just ran it as cout<<number[x]; right after the if statement and it only prints out 123.
For the same reason your code would miss a duplicate if there were 3 in a row, it will not print 4 in the loop if you output from within (depending on where the output is placed.) Loop through the vector after the loop to see what is still in the vector.
Thanks a lot that worked. I used the first loop to fix the vector, then i made a second one right after it that prints it out. It printed out 1234. Thanks again.