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不變。