#include#include using namespace std; int main() { vector nums; nums.reserveInitialCapacity(5); for (int i = 0; i < 5; i++) { nums.push_back(i+1); } for (auto num : nums) { cout << num << " "; } return 0; } Output: 1 2 3 4 5
#includeIn the example above, we create a vector of strings and reserve space for 3 elements using the reserveInitialCapacity method. We then add two names to the vector using the push_back method and print out the current size of the vector. Finally, we add a third name to the vector and print out the new size of the vector. Package/Library: Standard Template Library (STL)#include using namespace std; int main() { vector names; names.reserveInitialCapacity(3); names.push_back("Alice"); names.push_back("Bob"); cout << "Current size: " << names.size() << endl; names.push_back("Charlie"); cout << "Current size: " << names.size() << endl; return 0; } Output: Current size: 2 Current size: 3