Weird runtime error

WHy does this program work when the latter program doesn't?


#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> ivec;
vector<int>::size_type esize;
cin >> esize;
vector<int>::iterator iter = ivec.begin();\*it doesn't work when this is in the beginning of the code*\
for(vector<int>::size_type s = 0; s != esize; ++s)
ivec.push_back(0);

while(iter!=ivec.end())
{
*iter = 9;
iter++;
}
iter=ivec.begin();
while(iter != ivec.end())
{
cout <<*iter;
iter++;
}
return 0;
}

#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> ivec;
vector<int>::size_type esize;
cin >> esize;
for(vector<int>::size_type s = 0; s != esize; ++s)
ivec.push_back(0);
vector<int>::iterator iter = ivec.begin();\\but works when it's here
while(iter!=ivec.end())
{
*iter = 9;
iter++;
}
iter=ivec.begin();
while(iter != ivec.end())
{
cout <<*iter;
iter++;
}
return 0;
}
I would assume it is because in the case where it does not work there is nothing in the vector to be considered the 'beginning'...
Topic archived. No new replies allowed.