vector之rerve()和resize()
1、vector中的rerve()函数⽤来定义预留空间,改变capacity,不改变size。会去分配内存,但不会构造出对象。如果改变后的capacity⽐当前的capacity⼤,则capacity会变为改变后的capacity,反之capacity不变。
#include <string>
#include <vector>英语作文模板
#include <iostream>
using namespace std;
int main()
{
vector<int> vec;
cout<<"1) vec capacity is "<<vec.capacity()<<endl;
cout<<"1) vec size is "<<vec.size()<<endl;
for(int i=0;i<50;i++)
{
vec.push_back(i+1);
}
cout<<"2) vec capacity is "<<vec.capacity()<<endl;
cout<<"2) vec size is "<<vec.size()<<endl;
cout<<"3) vec capacity is "<<vec.capacity()<<endl;
cout<<"3) vec size is "<<vec.size()<<endl;
lucky star
for(int i=0;i<5;i++)
{
vec.push_back(100+i);
}
cout<<"4) vec capacity is "<<vec.capacity()<<endl;
cout<<"4) vec size is "<<vec.size()<<endl;
优点用英语怎么说ve(100);
cout<<"5) vec capacity is "<<vec.capacity()<<endl;
cout<<"5) vec size is "<<vec.size()<<endl;
性别英语
cout<<"6) vec capacity is "<<vec.capacity()<<endl;
cout<<"6) vec size is "<<vec.size()<<endl;
ordinary daychar ch[] = "abcdef";
char chh[] = {'a','b','c','d','e','f'};
cout<<"The length of ch is "<<strlen(ch)<<endl;
cout<<"The length of chh is "<<strlen(chh)<<endl;
system("pau");
noother
return 0;
}
输出结果:
cret什么意思
2、resize⽤来改变vector的size,有可能也会改变capacity。
如果改变后的size⽐当前capacity⼤,则capacity会变⼤,同时构造出多出来的对象;
反之,capacity不变,同时析构⼀些不再需要的对象。
cout<<"1) vec capacity is "<<vec.capacity()<<endl;
cout<<"1) vec size is "<<vec.size()<<endl;
四级词汇下载
cout<<"2) vec capacity is "<<vec.capacity()<<endl;
cout<<"2) vec size is "<<vec.size()<<endl;
cout<<"3) vec capacity is "<<vec.capacity()<<endl;
cout<<"3) vec size is "<<vec.size()<<endl;
输出:
veteran是什么意思rerve()和resize()都不会使capacity变⼩,但都有可能使capacity变⼤。
rerve()能准确控制capacity,⽽resize()不能,vc⾥是每次增⼤当前capacity的⼀半。
1)⽐如当前capacity是100,size(105);之后capacity变为150(增⼤50,100的⼀半),size变为105;若当前capacity是200,size(250);之后capacity变为300(增⼤100,200的⼀半),
size变为250.
2)单纯只改变capacity(),不会改变size的⼤⼩。
违约责任英文