Some years ago I had an introductory course to c++ at my university. However, I mainly used functional languages such as R or Matlab. Now I started again to learn c++. I was reading about vectors and run in the following:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> test1;
std::vector<int> test2(2);
// works perfectly as expected:
test2[0] = 1;
test2[1] = 2;
// this would give an error
//test1[0] = 1;
//instead I have to write
test1.push_back(1);
return 0;
}
If I use default initialization for test1, why do I have to use puch_back? Wouldn't it be "smarter" to use the [] operator and automatically insert the element? Why is this forbidden in c++?