std::vectormy_vec; my_vec.reserve(100); // reserve space for 100 elements
std::vectorIn this example, we create an empty vector of doubles and reserve space for 500 elements. We then use a for loop to add 500 elements to the vector, which should perform faster because the memory space has already been reserved. The `std::vector::reserve` method is part of the C++ standard library, which is included in the C++ programming language itself.my_vec2; my_vec2.reserve(500); for (int i = 0; i < 500; i++) { my_vec2.push_back(i * 0.5); // add elements to the vector }