Unexpected results when calling resize() on an STL vector in C++
In the following code, if I call v.resize(n), the program will print out 0
0 0 0 0 0 0 0 0 0, which is not what I wanted to see. However, if I
comment the line containing v.resize(n) out, it will print out 0 1 2 3 4 5
6 7 8 9, which is what I wanted to see. Why is this the case? What's wrong
with my logic here?
#include <iostream>
#include <vector>
using namespace std;
int main( int argc , char ** argv )
{
int n = 10;
vector<int> v;
v.resize(n);
for( int i=0 ; i<n ; i++ )
{
v.push_back(i);
}
for( int i=0 ; i<n ; i++ )
{
cout << v[i] << " ";
}
cout << endl;
return 0;
}
No comments:
Post a Comment