C++:STL vector:sizeof(vector)
原文地址:http://blog.csdn.net/zcsylj/article/details/7857009
int的大小是4,定义vector<int> vec,vec中有一个元素,sizeof(vec)=20,如果有1000个元素,则sizeof(vec)是多少?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec;
for(int i=;i<;i++)
{
vec.push_back(i);
cout<<sizeof(vec)<<endl;
cout<<vec.size()<<endl;
}
}
输出结果:
……
请按任意键继续. . .
由此可以看出:sizeof(vec)只取决于vector里面存放的数据类型,与元素个数无关。该值应该是与编译器相关的。
#include <iostream>
#include <vector>
using namespace std;
int main()
{
cout<<"sizeof(vector<char>) = "<<sizeof(vector<char>)<<endl;
cout<<"sizeof(vector<int>) = "<<sizeof(vector<int>)<<endl;
cout<<"sizeof(vector<short>) = "<<sizeof(vector<short>)<<endl;
cout<<"sizeof(vector<double>) = "<<sizeof(vector<double>)<<endl;
cout<<"sizeof(vector<long>) = "<<sizeof(vector<long>)<<endl;
cout<<"sizeof(vector<float>) = "<<sizeof(vector<float>)<<endl;
cout<<"sizeof(vector<bool>) = "<<sizeof(vector<bool>)<<endl;
cout<<"sizeof(vector<string>) = "<<sizeof(vector<string>)<<endl;
}
输出:
sizeof(vector<char>) =
sizeof(vector<int>) =
sizeof(vector<short>) =
sizeof(vector<double>) =
sizeof(vector<long>) =
sizeof(vector<float>) =
sizeof(vector<bool>) =
sizeof(vector<string>) =
请按任意键继续. . .
解释:
vector应该是从堆上分配内存,所有大小与元素个数无关
sizeof(vector)取决于vector类的具体实现,STL是个完全开放的东西,谁都可以来实现vector类。
通过查看STL源码可以看到vector有四个成员变量
_A allocator;
iterator _First, _Last, _End;
每个指针是4个字节,因此16字节。20字节的是不是添加了什么指针呢
C++:STL vector:sizeof(vector)的更多相关文章
- STL学习:STL库vector、string、set、map用法
本文仅介绍了如何使用它们常用的方法. vector 1.可随机访问,可在尾部插入元素:2.内存自动管理:3.头文件#include <vector> 1.创建vector对象 一维: (1 ...
- STL review:vector &; string &; map &; struct
I.vector 1.头文件:#include<vector> //容器vector是一个能实现随机存取.插入删除的动态数组,还可以当栈使. ...
- paper 5:支持向量机系列二: Support Vector —— 介绍支持向量机目标函数的 dual 优化推导,并得出“支持向量”的概念。
paper 4中介绍了支持向量机,结果说到 Maximum Margin Classifier ,到最后都没有说“支持向量”到底是什么东西.不妨回忆一下上次最后一张图: 可以看到两个支撑着中间的 ga ...
- vector:动态数组
vector是C++标准模板库中的部分内容,中文偶尔译作“容器”,但并不准确.它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存 ...
- Java集合系列(二):ArrayList、LinkedList、Vector的使用方法及区别
本篇博客主要讲解List接口的三个实现类ArrayList.LinkedList.Vector的使用方法以及三者之间的区别. 1. ArrayList使用 ArrayList是List接口最常用的实现 ...
- STL顺序容器【vector】【deque】【list】
我们都知道,stl在集装箱船分为两类,订购集装箱和相关的容器. 顺序容器有三种即动态数组vector,双端队列deque,以及链表list (对csdn的文字排版严重吐槽.写好的版发表了就变了) 一: ...
- C++复习:STL之算法
算法 1算法基础 1.1算法概述 算法部分主要由头文件<algorithm>,<numeric>和<functional>组成. <algorithm> ...
- STL之序列容器vector
首先来看看vector的模板声明: template <class T, class Alloc = allocator<T>> class vector { //… }; v ...
- UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)
Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...
随机推荐
- CPrimerPlus第11章第10题
题目: 编写一个程序,读取输入,直到读入了10个字符串或遇到EOF,由二者中最先被满足的那个终止读取过程.这个程序可以为用户提供一个有5个选项的菜单:输出初始字符串列表.按ASCII顺序输出字符串.按 ...
- CISCO系列交换机如何清空配置
今天刚好要还原一台CISCO配置的命令,这个毕竟属于我的专业小编就献丑一回,望大家见谅! 工具/原料 CISCO系列交换 方法/步骤 1 首先备份之前的配置信息 copy flash:conf ...
- win7下利用笔记本无线网卡创建AP 组建无线局域网(可以连魔兽,TCP、UDP也没问题)
转自:http://blog.163.com/fghok_018/blog/static/122599670201072773924530/ 近一个月,宿舍的好多同学都买了笔记本电脑,当然,我也买了, ...
- 转载 radio值获取
选择控件:select ,radio,checkbox之用jquery获取选中值的小结 博客分类: jQuery select下拉框radio单选按钮checkbox多选框jquery获取选中的值 ...
- inuitcss
inuitcssa powerful, scalable, Sass-based, BEM, OOCSS framework.
- Ojbect-C2 3、NSArray和NSMutableArray数组的使用
Adopted Protocols NSCoding encodeWithCoder: initWithCoder: NSCopying copyWithZone: NSMutableCopying ...
- bootstrap学习之利用CSS属性pointer-events禁用表单控件
参考链接: CSS3 pointer-events:none应用举例及扩展 首先pointer-events在除去SVG中的应用只有两个值:AUTO | NONE pointer-events:non ...
- IE11开发人员工具 js脚本debugger调试
微软在浏览器IE11中对F12开发人员工具的UI进行了彻底的换脸,虽然有诸多的不习惯但也是有方便之处的,比如我们用到的最多的断点调试,在F12调试状态下会加载出页面的js脚本文件,而需要对某个文件中的 ...
- python+selenium—webdriver入门(二)
本文中主要介绍webdriver常见的对象定位方法: 一.对象定位的目的 二.常见的对象定位方法 一.对象定位的目的: 1.操作对象 2.获得对象的属性,如:对象的class属性.name属性等 3. ...
- P4592 [TJOI2018]异或
吐槽 睡起来写道模板清醒一下 貌似有两个log的树剖写法,还有一个log的Trie树的差分做法(类似于count on a tree),然后一个log的要把两个询问分开写,一个dfs序一个差分,然后我 ...