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. hive中sql解析出对应表和字段的实现

    import java.io.IOException; import java.util.HashMap; import java.util.HashSet; import java.util.Map ...

  2. HTML5与CSS3权威指南.pdf9

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

  3. js函数参数设置默认值

    php有个很方便的用法是在定义函数时可以直接给参数设默认值,如: function simue ($a=1,$b=2){  return $a+$b;}echo simue(); //输出3echo ...

  4. 3 weekend110的shuffle机制 + mr程序的组件全貌

    前面,讲到了hadoop的序列化机制,mr程序开发,自定义排序,自定义分组. 有多少个reduce的并发任务数可以控制,但有多少个map的并发任务数还没 缓存,分组,排序,转发,这些都是mr的shuf ...

  5. hdoj 1862 EXCEL排序

    EXCEL排序 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. asp.net mvc 提交model 接收不了

    [HttpPost]        //[ValidateInput(false)]        public ActionResult AddNews1(_54Young_News_Model.m ...

  7. EasyUI-页面布局

    通过使用 jQuery EasyUI 可以很容易地添加 Tabs.您只需要调用 'add' 方法即可. 在本教程中,我们将使用 iframe 动态地添加显示在一个页面上的 Tabs. 当点击添加按钮, ...

  8. Pilin —— 一个基于Xmpp openfire smack的即时聊天工具

    https://github.com/whfcomm/Pilin

  9. 如何判断Android系统的版本

    随着Android版本的增多,在不同的版本中使用不同的设计是必须的,根据程序运行的版本来提供不同的功能.这涉及到如何在程序中判断Android系统的版本. 在Android api中的android. ...

  10. android开发之Bundle使用

    android开发中,我们经常需要在两个activity之间传递数据,最常用的莫过于使用intent.putXXX(),可是很多时候我们也会这样: Bundle bundle = new Bundle ...