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

vector预分配空间溢出的更多相关文章

  1. vecor预分配内存溢出2

    vector预分配内存溢出导致原始的 迭代器 失效 consider what happens when you add the one additional object that causes t ...

  2. Java堆空间溢出解决方法 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

    一般通过java -jar filename.jar运行jar包,但是当运行的java程序需要较大的内存时,可能会造成堆空间溢出. 例如,加载了几个G大小的json文件,运行报错: Exception ...

  3. STL—vector空间的动态增长

    vector空间的动态增长     当添加元素时,如果vector空间大小不足,则会以原大小的两倍另外配置一块较大的新空间,然后将原空间内容拷贝过来,在新空间的内容末尾添加元素,并释放原空间.vect ...

  4. STL空间配置器、vector、list、deque、map复习

    本文写于2017-03-03,从老账号迁移到本账号,原文地址:https://www.cnblogs.com/huangweiyang/p/6440830.html STL的六大组件:容器.算法.迭代 ...

  5. vector中的resize与 reserve

    void reserve (size_type n); reserver函数用来给vector预分配存储区大小,即capacity的值 ,但是没有给这段内存进行初始化.reserve 的参数n是推荐预 ...

  6. stl——vector详解

    stl——vector详解 stl——vector是应用最广泛的一种容器,类似于array,都将数据存储于连续空间中,支持随机访问.相对于array,vector对空间应用十分方便.高效,迭代器使ve ...

  7. java内存溢出的解决思路

    原文地址:https://www.cnblogs.com/200911/p/3965108.html 内存溢出是指应用系统中存在无法回收的内存或使用的内存过多,最终使得程序运行要用到的内存大于虚拟机能 ...

  8. vector at()函数比 []运算符操作安全

    转载:https://blog.csdn.net/chenjiayi_yun/article/details/18507659 []操作符的源码 reference operator[](size_t ...

  9. 【校招面试 之 C/C++】第20题 C++ STL(二)之Vector

    1.vector的动态增长 当添加元素时,如果vector空间大小不足,则会以原大小的两倍另外配置一块较大的新空间,然后将原空间内容拷贝过来,在新空间的内容末尾添加元素,并释放原空间.vector的空 ...

随机推荐

  1. PCB覆铜时的安全距离

    覆铜的安全间距(clearance)一般是布线的安全间距的二倍.但是在没有覆铜之前,为布线而设置好了布线的安全间距,那么在随后的覆铜过程中,覆铜的安全间距也会默认是布线的安全距离.这样与预期的结果不一 ...

  2. background-attachment:fixed应用

    设置为fixed属性,背景相对于屏幕窗口固定,然后如果有一张全屏的图片,再来一张全屏的图片,就可以看到与平时滚动屏幕不同的切换图片.代码CSS部分: html, body,.content{ heig ...

  3. HTML5与CSS3权威指南.pdf9

    第21章 Media Queries相关样式 该模块中允许添加媒体查询(media query)表达式,用以指定媒体类型,根据媒体类型来选择使用的样式(显示器.便携设备.电视机) 使用方法@media ...

  4. 依賴注入入門——Unity(二)

    參考博客文章http://www.cnblogs.com/kebixisimba/category/130432.html http://www.cnblogs.com/qqlin/tag/Unity ...

  5. SecureCRT 实用配置

    SecureCRT,是一款支持 SSH2.SSH1.Telnet.Telnet/SSH.Relogin.Serial.TAPI.RAW 等协议的终端仿真程序,最吸引我的是,SecureCRT 支持标签 ...

  6. 西安Uber优步司机奖励政策(2月1日~2月7日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  7. Struct标签

    通用标签:  1. property                2. set i.          默认为action scope,会将值放入request和ActionContext中 ii. ...

  8. 手机变为电脑的摄像头,使像素高清起来-使用DroidCam

    你是不是已经在嫌弃电脑自带的摄像头的渣渣像素呢? 今天给大家安利一个方法:将手机摄像头设置为电脑的摄像头,让像素高清起来,对于搞图像的同志们真是福音啊,尤其是做人脸识别的时候. 方法有很多种,我推荐我 ...

  9. Expression Blend 4 激活码

    Expression Blend 4 激活码: 6WDDQ-K7D4F-GQGF4-2VYBJ-8K6MB

  10. ubuntu 14.04 chromium,firefox 怎样正确安装Adobe flash player

    一.firefox 正确安装Adobe flash player 有时候我们须要在Ubuntu下採用手动安装一些软件,比方Firefox的Flash插件.Adobe® Flash® Player 是一 ...