vector

当一个vector预分配的存储空间用完之后,为维护其连续的对象数组,它必须在另外一个地方重新分配大块新的(更大的)存储空间,并把以前已有的对象拷贝到新的存储空间中去。

// A class to track various object activities
#ifndef NOISY_H
#define NOISY_H
#include<iostream>
using std::endl;
using std::cout;
using std::ostream;
class Noisy{
//设置一些static变量用来跟踪所有的创建,赋值(使用运算符operator=), 拷贝构造和析构操作
static long create, assign, copycons, destroy;
long id;
public:
Noisy() :id(create++)//
{
cout << "d[" << id << "]" << endl;
}
Noisy(const Noisy& rv) :id(rv.id)
{
cout << "c[" << id << "]" << endl;
++copycons;
}
Noisy& operator=(const Noisy& rv){//赋值运算符operator=
cout << "(" << id << ")=[" << rv.id << "]" << endl;
id = rv.id;
++assign;
return *this;
}
//为了支持排序和查找,Noisy 必须有运算符operator< 和 operator=
//这仅仅是比较id值
friend bool operator==(const Noisy& lv, const Noisy& rv)
{
return lv.id == rv.id;
}
friend bool operator<(const Noisy& lv, const Noisy& rv)
{
return lv.id < rv.id;
}
~Noisy(){
cout << "~[" << id << "]" << endl;
++destroy;
}
friend ostream& operator<<(ostream& os, const Noisy& n)
{
return os << n.id;
}
friend class NoisyReport;
};
//Ojects of type NoisyGen are funciton objects(since there is an operator())
//that produce Noisy objects during testing
struct NoisyGen{
Noisy operator()(){
return Noisy();
}
};
//a singleton will automatically report the statistics as the program terminates
class NoisyReport{
static NoisyReport nr;
NoisyReport(){}//private constructor
NoisyReport& operator=(NoisyReport&);//disallowed
NoisyReport(const NoisyReport&);//disallowed
public:
//beause we only want one report printed at program termination.
//It has a private constructor so no additional NoisyReport objects can be created
//it disallows assignment and copy-construction,
//and it has a single static instance of NoisyReport called nr.
//the only executable statements are in the destructor, which is called as the program
//exits
//and static destructors are called.
//the distructor printss the statistics captured by the static variables in Noisy
~NoisyReport()
{
cout << "\n.................\n"
<< "Noisy creations: " << Noisy::create
<< "\nCopy-Constructions: " << Noisy::copycons
<< "\nAssignment: " << Noisy::assign
<< "\nDestructions: " << Noisy::destroy << endl;
}
};
#endif
#include"Noisy.h"
long Noisy::create = , Noisy::assign = , Noisy::copycons = , Noisy::destroy = ;
NoisyReport NoisyReport::nr;
//using Noisy.h, the following program shows a vector overflowing
#include<iostream>
#include<string>
#include<vector>
#include<cstdlib>
#include"Noisy.h"
using namespace std;
int main(int argc, char* argv[])
{
int size = ;
if (argc >= ) size = atoi(argv[]);
vector<Noisy> vn;
Noisy n;
for (int i = ; i < size; i++)
vn.push_back(n);
cout << "\n clearning up" << endl; }

测试结果


Note that the use of reserve( ) is different from using the vector constructor with an integral first argument; the latter initializes a prescribed number of elements using the element type’s default constructor.

是不是说

vector<Noisy> v(100)这用形式调用100次构造函数

而reverse其实是不调用的,只预留空间

The deque also allows random access with operator[ ], but it’s not quite as fast as vector’s operator[ ].

最新文章

  1. Linux系统学习优缺点
  2. java.lang.NoClassDefFoundError: javax/el/ELResolver 问题解决
  3. 把notepad++设置为系统全局文本默认打开应用
  4. nginx配置404
  5. C++-什么时候需要在类的构造函数中使用初始化列表
  6. Failed to load IDE add in &#39;C:\Program Files\Delphi_2007\bin\Borland.Studio.Together.dll&#39;.解决办法 转
  7. PhotoShop—剪贴蒙版
  8. js中的eval方法转换对象时,为何一定要加上括号?
  9. Swift供选链接
  10. Android----paint触摸轨迹监听
  11. easyui项目问题集锦
  12. 常用OJ名字+地址(自用)
  13. 正则表达式,提取html标签的属性值
  14. IDF-cookie欺骗
  15. django - 总结 - 中间件
  16. python下调用pytesseract识别某网站验证码
  17. 访问内网(https,udp)
  18. Spring的quartz定时器重复执行二次的问题解决
  19. phone手机比较
  20. Spring下获取项目根路径--good

热门文章

  1. 使用socket.io打造公共聊天室
  2. 作品第一课----获取批量checkbox选中的值
  3. arm汇编--ubuntu12.04 安装arm-linux交叉编译环境
  4. 一个空格引发的bug
  5. SignalR及时通知功能
  6. IO 流—&gt;&gt;&gt;补充
  7. Mysql分表和分区的区别
  8. Oracle学习.Windows 命令行 启动ORACLE服务与实例
  9. VIM中的正则表达式及替换命令
  10. Android APK反编译具体解释(附图)