C++笔记之vector的reserve()和capacity()用法
code review!
代码
#include <vector>
#include <iostream>
int main() {
std::vector<int> myVector;
std::cout << "Current size: " << myVector.size() << std::endl;
std::cout << "Current capacity: " << myVector.capacity() << std::endl;
myVector.reserve(100); // 预留至少能存储 100 个元素的内存空间
std::cout << "After reserving 100: " << std::endl;
std::cout << "Current size: " << myVector.size() << std::endl;
std::cout << "Current capacity: " << myVector.capacity() << std::endl;
return 0;
}