vector的reallocation
来源:互联网 发布:php jsonp callback 编辑:程序博客网 时间:2024/06/06 02:20
每一个面试题都需要用心
#include <iostream>#include <vector>using namespace std;class pt{public: pt() { cout << "construction" << endl; } pt(const pt &obj) { cout << "copy construction" << endl; } ~pt() { cout << "destruction" << endl; }};int main(){ pt a; pt b; vector<pt> vec; vec.push_back(a); vec.push_back(b); vec.push_back(a); vec.push_back(b); cout << vec.size() << endl;}
输出答案:
construction
construction
copy construction
copy construction
copy construction
destruction
copy construction
copy construction
copy construction
destruction
destruction
copy construction
4
destruction
destruction
destruction
destruction
destruction
destruction
输出答案分析:
- vector可用空间不足时空间增长算法。增长到最近的2^n大小,即按1、2、4、… 、增长。
- vector空间重新分配时采用copy构造移动以前的元素,然后集中destruction。
参考:
vector push_back()官方帮助文档
Add element at the end
Adds a new element at the end of the vector, after its current last element. The content of val is copied (or moved) to the new element.
This effectively increases the container size by one, which causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity.
阅读全文
0 0
- vector的reallocation
- Vector::Vector的定义
- Vector,和vector的应用
- vector<vector<int>>的输入
- vector<vector<int> >的输入输出
- 关于vector的vector<vector<int>>的使用问题
- vector<int>G[] 和 vector<vector<int> G 的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector<char*>和vector<string>的不同
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- LocalBroadcastManager机制分析
- 遇到多个构造器参数时要考虑用构建器
- ES5 Array
- leetcode 381. Insert Delete GetRandom O(1)
- POJ1149 PIGS
- vector的reallocation
- 15.Nginx 服务器的高级配置
- k-近邻
- CentOS_6.5下安装mysql_5.5.48
- maven常见错误
- magic squire幻方的学习——奇数阶幻方
- StarUML破解教程
- sddsf
- 反射机制