#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec = {1, 2, 3};
vec.push_back(5);
cout << vec.size() << endl;
cout << vec.capacity() << endl;
return 0;
}
此时输出是 4 4;
代码为
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec = {1, 2, 3, 4};
vec.push_back(5);
cout << vec.size() << endl;
cout << vec.capacity() << endl;
return 0;
}
此时输出是 5 6;
感觉扩容好像是2的倍数这种,这是为什么?