There is a thread in the comments section in this post about using std::vector::reserve() vs. std::vector::resize().
Here is the original code:
void MyClass::my_method() { my_member.reserve(n_dim); for(int k = 0 ; k < n_dim ; k++ ) my_member[k] = k ; } I believe that to write elements in the vector, the correct thing to do is to call std::vector::resize(), not std::vector::reserve().
In fact, the following test code "crashes" in debug builds in VS2010 SP1:
#include <vector> using namespace std; int main() { vector<int> v; v.reserve(10); v[5] = 2; return 0; } Am I right, or am I wrong? And is VS2010 SP1 right, or is it wrong?
resize(), and the doubt was cleared. To moderators: feel free to delete this question if it's "too localized", or keep it if you think that it might help someone else in the future.