1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 vector<int> v1(10); 9 10 std::cout << "v1 size : " << v1.size() << std::endl; 11 std::cout << "v1 capacity : " << v1.capacity() << std::endl; 12 13 v1.reserve(20); 14 15 std::cout << "v1 size : " << v1.size() << std::endl; 16 std::cout << "v1 capacity : " << v1.capacity() << std::endl; 17 18 return 0; 19 }
此函数用来改变容量大小,运行结果如下:
capacity扩大到了20,size不变。