vector预留空间(reserve)
作者:互联网
#include <iostream>
#include <vector>
using namespace std;
#define NUM 10000
int main()
{
vector<int> v;
int *p = NULL;
int num = 0;
for(int i = 0; i < NUM; i++)
{
v.push_back(i);
if(p != &v[0])
{
p = &v[0];
num++;
}
}
cout << "num " << num << endl;
return 0;
}
$ ./a.out
num 15
#include <iostream>
#include <vector>
using namespace std;
#define NUM 10000
int main()
{
vector<int> v;
int *p = NULL;
int num = 0;
v.reserve(NUM);
for(int i = 0; i < NUM; i++)
{
v.push_back(i);
if(p != &v[0])
{
p = &v[0];
num++;
}
}
cout << "num " << num << endl;
return 0;
}
$ ./a.out
num 1
标签:vector,num,int,++,NUM,预留,include,reserve 来源: https://www.cnblogs.com/zhangxuechao/p/16526253.html