vector容器成员函数resize与reserve的区别
2013-09-20 15:35
423 查看
resize是设置向量的大小:即向量的大小与容量相同。而reserve是设置向量的容量,并不改变向量的大小。另外:向量的最大大小是固定住,不可改变。例子:
#include
"stdafx.h"
#include
<iostream>
#include
<vector>
using namespace std;
typedefvector<int> VEC_INT;
int_tmain(int argc, _TCHAR* argv[])
{
VEC_INTthevec;
cout<<"before insert data:"<<endl;
cout<<"the size of vector:"<<thevec.size()<<endl;
cout<<"the capasity of vector:"<<thevec.capacity()<<endl;
cout<<"the max size of vector:"<<thevec.max_size()<<endl;
cout<<"after insert data:"<<endl;
thevec.push_back(42);
cout<<"the size of vector:"<<thevec.size()<<endl;
cout<<"the capasity of vector:"<<thevec.capacity()<<endl;
cout<<"the max size of vector:"<<thevec.max_size()<<endl;
thevec.reserve(100);
cout<<"After reserve (100):"<<endl;
cout<<"the size of vector:"<<thevec.size()<<endl;
cout<<"the capasity of vector:"<<thevec.capacity()<<endl;
cout<<"the max size of vector:"<<thevec.max_size()<<endl;
thevec.resize(1000);
cout<<"After resize (1000):"<<endl;
cout<<"the size of vector:"<<thevec.size()<<endl;
cout<<"the capasity of vector:"<<thevec.capacity()<<endl;
cout<<"the max size of vector:"<<thevec.max_size()<<endl;
return 0;
}
运行结果:
![](http://img.blog.csdn.net/20130920153420312?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWFzdGVyX2ZlbmcyMDA5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
可以看出:默认情况下,向量的大小和容量是相同的,如果用resize改变向量的大小,则同时改变了向量的容量。
#include
"stdafx.h"
#include
<iostream>
#include
<vector>
using namespace std;
typedefvector<int> VEC_INT;
int_tmain(int argc, _TCHAR* argv[])
{
VEC_INTthevec;
cout<<"before insert data:"<<endl;
cout<<"the size of vector:"<<thevec.size()<<endl;
cout<<"the capasity of vector:"<<thevec.capacity()<<endl;
cout<<"the max size of vector:"<<thevec.max_size()<<endl;
cout<<"after insert data:"<<endl;
thevec.push_back(42);
cout<<"the size of vector:"<<thevec.size()<<endl;
cout<<"the capasity of vector:"<<thevec.capacity()<<endl;
cout<<"the max size of vector:"<<thevec.max_size()<<endl;
thevec.reserve(100);
cout<<"After reserve (100):"<<endl;
cout<<"the size of vector:"<<thevec.size()<<endl;
cout<<"the capasity of vector:"<<thevec.capacity()<<endl;
cout<<"the max size of vector:"<<thevec.max_size()<<endl;
thevec.resize(1000);
cout<<"After resize (1000):"<<endl;
cout<<"the size of vector:"<<thevec.size()<<endl;
cout<<"the capasity of vector:"<<thevec.capacity()<<endl;
cout<<"the max size of vector:"<<thevec.max_size()<<endl;
return 0;
}
运行结果:
可以看出:默认情况下,向量的大小和容量是相同的,如果用resize改变向量的大小,则同时改变了向量的容量。
相关文章推荐
- STL中容器的几个容易混淆的成员函数-size, capacity, resize, reserve
- STL中容器的几个容易混淆的成员函数-size, capacity, resize, reserve
- vector的resize()和reserve()函数的区别
- STL中容器的几个容易混淆的成员函数-size, capacity, resize, reserve
- STL: vector容器成员函数resize的作用
- vector的resize()和reserve()函数的区别
- C++ Daily《2》----vector容器的resize 与 reserve的区别
- C++ Daily《2》----vector容器的resize 与 reserve的区别
- vector.resize 与 vector.reserve的区别
- vector中resize()和reserve()区别
- vector.resize 与 vector.reserve的区别
- STL容器的reserve()函数和resize()函数解析
- STL-- vector中resize()和reserve()区别
- C++容器vector的常用成员函数
- C++ vector中size/resize和capacity/reserve的区别
- c++中vector的 resize()和reserve()区别
- vector容器的resize和reserve
- vector的reserve和resize的区别
- C++ vector与数组区别 reserve与resize区别
- vector resize reserve区别