标签:c++11 库 内存 vector对象时如何增长的
shrink_to_fit //只适用于vector,string,deque capacity reserve //只适用于vector,string c.shrink_to_fit() //请将capacity()减少为与size相同大小 c.capacity() //不重新分配内存空间的话,c可以保存多少元素 c.reserve(n) //分配至少能容纳n个元素的内存空间
#include<iostream> #include<vector> using namespace std; int main() { vector<int> ivec; //size应该为0;capacity的值依赖于具体实现 cout<<" ivec : size: "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl; //向ivec添加24个元素 for(vector<int>::size_type ix=0 ; ix != 24 ; ++ix) ivec.push_back(ix); //size应该为24;capacity应该大于或等于24,具体值依赖于标准库实现 cout<<" ivec: size: "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl; }
ivec: size: 0 capacity: 0
ivec: size: 24 capacity: 32
ivec.reserve(50); //将capacity至少设定为50,可能会更大 //size应该为24;capacity应该大于等于50,具体值依赖于标准库 cout<<" ivec :size: "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl;
ivec: size: 24 capacity: 50
//添加元素用光多余容量 while(ivec.size() != ivec.capacity()) ivec.push_back(0); //capacity应该未改变,size和capacity不相等 cout<<" ivec : size : "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl;
ivec: size: 50 capacity: 50
ivec.push_back(42); //添加一个元素 //size的值现在应是51;capacity应该大于51,看以来的标准库 cout<<" ivec : size : "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl;
ivec: size: 51 capacity: 100
ivec.shrink_to_fit(); //要求归还内存 //size未变capacity应该看具体实现 cout<<" ivec : size : "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl;
ivec: size: 51 capacity: 51
vector<int> ivec; //size应该为0;capacity的值依赖于具体实现 //添加256个单词 for(vector<int>::size_type ix=0 ; ix != 256 ; ++ix) { ivec.push_back(ix); } cout<<" ivec : size : "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl; ivec.resize(500); //吧它capacity设定为至少500 cout<<" ivec : size : "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl; ivec.shrink_to_fit(); //返回内存 for(int ix=0 ; ix != 12 ; ++ix) ivec.push_back(ix); cout<<" ivec : size : "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl;
/** * 功能:vector对象时如何增长的 * 时间:2014年6月12日07:43:54 * 作者:cutter_point */ /* shrink_to_fit //只适用于vector,string,deque capacity reserve //只适用于vector,string c.shrink_to_fit() //请将capacity()减少为与size相同大小 c.capacity() //不重新分配内存空间的话,c可以保存多少元素 c.reserve(n) //分配至少能容纳n个元素的内存空间 */ #include<iostream> #include<vector> using namespace std; int main() { /* vector<int> ivec; //size应该为0;capacity的值依赖于具体实现 cout<<" ivec : size: "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl; //向ivec添加24个元素 for(vector<int>::size_type ix=0 ; ix != 24 ; ++ix) ivec.push_back(ix); //size应该为24;capacity应该大于或等于24,具体值依赖于标准库实现 cout<<" ivec: size: "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl; ivec.reserve(50); //将capacity至少设定为50,可能会更大 //size应该为24;capacity应该大于等于50,具体值依赖于标准库 cout<<" ivec :size: "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl; //添加元素用光多余容量 while(ivec.size() != ivec.capacity()) ivec.push_back(0); //capacity应该未改变,size和capacity不相等 cout<<" ivec : size : "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl; ivec.push_back(42); //添加一个元素 //size的值现在应是51;capacity应该大于51,看以来的标准库 cout<<" ivec : size : "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl; ivec.shrink_to_fit(); //要求归还内存 //size未变capacity应该看具体实现 cout<<" ivec : size : "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl; */ vector<int> ivec; //size应该为0;capacity的值依赖于具体实现 //添加256个单词 for(vector<int>::size_type ix=0 ; ix != 256 ; ++ix) { ivec.push_back(ix); } cout<<" ivec : size : "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl; ivec.resize(500); //吧它capacity设定为至少500 cout<<" ivec : size : "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl; ivec.shrink_to_fit(); //返回内存 for(int ix=0 ; ix != 12 ; ++ix) ivec.push_back(ix); cout<<" ivec : size : "<<ivec.size() <<" capacity: "<<ivec.capacity()<<endl; return 0; }
【足迹C++primer】27、vector对象是如何增长的,布布扣,bubuko.com
【足迹C++primer】27、vector对象是如何增长的
标签:c++11 库 内存 vector对象时如何增长的
原文地址:http://blog.csdn.net/cutter_point/article/details/30210167