I wrote the following program for pushing and printing values into a vector. It seems that if I push as well as print values from a vector, it gives me undefined behaviour. For example, my expected output should have been: (1, 3, 4, 5) while I am getting the output as (0, 3, 4, 5). Can someone please explain as to where am I going wrong.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<unsigned> vec;
vec.push_back(1); vec.push_back(3); vec.push_back(4); vec.push_back(5);
for(vector<unsigned>::iterator i=vec.begin(), l=vec.end(); i!=l; ++i){
vec.push_back(2);
cout<<(*i)<<"\n";
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire