vector预分配空间溢出

时间:2022-10-21 19:40:20

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; }

测试结果

vector预分配空间溢出


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[ ].