I use the reserve function to pre-allocate 10 double-sized memory, and then use the operator[] method to assign values, and the data can be printed out through std::cout, but why does size() is 0?
(Note: There are several solutions, but I want to know why this happens?)
The code is here:
#include <iostream>
#include <vector>
int main(int argc, char* argv[]) {
std::vector<double> test;
test.reserve(10);
for (int i = 0; i < 10; ++i) {
test[i] = i * 12;
}
for (int i = 0; i < 10; ++i) {
std::cout << test[i] << std::endl;
}
std::cout << "the size is: " << test.size() << std::endl;
std::cout << "the capacity is: " << test.capacity() << std::endl;
return 0;
}
The out is below:
0 12 24 36 48 60 72 84 96 108
the size is: 0
the capacity is: 10