I was doing some exercise from C++ Primer and one of them required us to copy a set of value from array into
a vector container. Then, we are suppose to remove all the even numbers from the vector container.
Hoever, when I tried to display them out by incrementing the iterator instead of using a normal for loop, I actually had obtained additional garbage values.
Here is my code:
#include <vector>
#include <list>
#include <iostream>
int main()
{
std::vector<int>vec;
std::list<int>li;
int ia[]={0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89};
for(int i(0); i<sizeof(ia);++i)
{
vec.push_back(ia[i]);
li.push_back(ia[i]);
}
auto vecIterator = vec.begin();
while(vecIterator!=vec.end())
{
if(*vecIterator%2)
++vecIterator;
else
vec.erase(vecIterator);
}
vecIterator = vec.begin();
while(vecIterator!=vec.end())
{
std::cout << *vecIterator <<'\n';
++vecIterator;
}
}
This is the array that I am supposed to copy into my vectorint ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
Here is the output from my terminal
1,1,3,5,8,13,21,55,89,32767, -3489211, 32467, -34895
Helpppppp!!!!