网上一大堆内容感觉其实也没有嚼一遍英文来得快而爽:
void reserve (size_type n);
Request a change in capacity
Requests that the
vector capacity be at least enough to contain
n elements.
If n is greater than the current vector capacity, the function causes the container to reallocate its storage increasing its capacity to n (or greater).
In all other cases, the function call does not cause a reallocation and the vector capacity is not affected.
This function has no effect on the vector size and cannot alter its elements. reserve请求改变容器的容量(注意是请求,通常我们请求一个人,那个人也不一定会100%答应吧?所以请求二字说明reserve可能不会得到实际响应)
请求容器的容量至少有可以容纳n个元素
如果:n>容器当前容量,reserve将导致容器扩充容量至n(或更多),注意这只是扩容,并没有对扩容的空间初始化,对这些刚扩容的空间是访问不到的
其他所有情况: n<=容器当前容量,包括n=0,reserve并不会导致容器重新分配内容,容器容量也不会改变(注意容器容量不会降低为n), 此时相当于请求无响应
无论那种情形,reserve都不会影响容器已有的元素,即在调用reserve前容器有size()个元素,调用reserve后还有size()个元素并且这些元素值没有改变!
void resize (size_type n, value_type val = value_type());
Change size
Resizes the container so that it contains
n elements.
If n is smaller than the current container size, the content is reduced to its first n elements, removing those beyond (and destroying them).
If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of n. If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized.
If n is also greater than the current container capacity, an automatic reallocation of the allocated storage space takes place.
Notice that this function changes the actual content of the container by inserting or erasing elements from it.
resize使容器内有n个元素
如果: n<size(),容器的size()缩减为为n,多余的元素(size()-n那部分元素)将被移除,但是容器的容量capacity不会改变
如果: n>size(),容器size()变大至n,新添加的元素将由val初始化,注意resize会初始化扩充的空间,而reserve只是扩充空间而已。
如果: n>capacity()容器当前容量,会诱发分配空间
注意,resize会通过添加或移除元素来改变容器的内容(容器的元素个数和元素值,扩充时新元素为val,缩减时移除元素)
验证代码:
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
void print(const vector<int>& vec)
{
cout<<"vector contains: ";
for(vector<int>::const_iterator it=vec.begin();it!=vec.end();it++)
cout<<*it<<" ";
cout<<endl;
}
int main(){
{
{//reserve(n),n小于容器容量
vector<int> vec{1,2,3};//c++11
print(vec);//vector contains: 1 2 3
cout<<"size()="<<vec.size()<<" capcaity()="<<vec.capacity()<<endl;//size()=3 capcaity()=3
//reserve()
vec.reserve(2);//n小于capacity什么也不影响
cout<<"size()="<<vec.size()<<" capcaity()="<<vec.capacity()<<endl;//size()=3 capcaity()=3
print(vec);//vector contains: 1 2 3,reserve不会影响容器已有元素
cout<<endl;
}
{//reserve(n),n大于容器容量
vector<int> vec{1,2,3};//c++11
print(vec);//vector contains: 1 2 3
cout<<"size()="<<vec.size()<<" capcaity()="<<vec.capacity()<<endl;//size()=3 capcaity()=3
//reserve()
vec.reserve(4);//n大于capacity会改变容器的capacity,诱发分配空间行为
cout<<"size()="<<vec.size()<<" capcaity()="<<vec.capacity()<<endl;//size()=3 capcaity()=4,capacity从3变为4
print(vec);//vector contains: 1 2 3,新分配的空间的元素并未初始化不能访问到,且不会改变容器的size()
cout<<endl;
}
}
{
{//resize(n)小,n于当前已有元素
vector<int> vec{1,2,3};//c++11
print(vec);//vector contains: 1 2 3
cout<<"size()="<<vec.size()<<" capcaity()="<<vec.capacity()<<endl;//size()=3 capcaity()=3
//reserve()
vec.resize(2);
cout<<"size()="<<vec.size()<<" capcaity()="<<vec.capacity()<<endl;//size()=2 capcaity()=3,size从3变为2
print(vec);//vector contains: 1 2,多余的元素被移除,容器capacity不会改变
cout<<endl;
}
{//resize(n),n大于当前已有元素
vector<int> vec{1,2,3};//c++11
print(vec);//vector contains: 1 2 3
cout<<"size()="<<vec.size()<<" capcaity()="<<vec.capacity()<<endl;//size()=3 capcaity()=3
//reserve()
vec.resize(4);//导致分配空间且初始化新扩张的元素部分
cout<<"size()="<<vec.size()<<" capcaity()="<<vec.capacity()<<endl;//size()=4 capcaity()=6,2倍扩张策略,所以capacity为6原来size()*2=3*2
print(vec);//vector contains: 1 2 3 0
cout<<endl;
}
{//resize(n),n小于当前容量
vector<int> vec{1,2,3};//c++11
vec.reserve(5);//使容器capacity达到5
print(vec);//vector contains: 1 2 3
cout<<"size()="<<vec.size()<<" capcaity()="<<vec.capacity()<<endl;//size()=3 capcaity()=5
//reserve()
vec.resize(4);
cout<<"size()="<<vec.size()<<" capcaity()="<<vec.capacity()<<endl;//size()=4 capcaity()=5,resize(n)中n小于capacity不会改变capacity
print(vec);//vector contains: 1 2 3 0
cout<<endl;
}
}
return 0;
}
程序输出:
vector contains: 1 2 3size()=3 capcaity()=3
size()=3 capcaity()=3
vector contains: 1 2 3
vector contains: 1 2 3
size()=3 capcaity()=3
size()=3 capcaity()=4
vector contains: 1 2 3
vector contains: 1 2 3
size()=3 capcaity()=3
size()=2 capcaity()=3
vector contains: 1 2
vector contains: 1 2 3
size()=3 capcaity()=3
size()=4 capcaity()=6
vector contains: 1 2 3 0
vector contains: 1 2 3
size()=3 capcaity()=5
size()=4 capcaity()=5
vector contains: 1 2 3 0