原文地址: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)的更多相关文章

  1. c++ :STL

    基础知识 容器 容器就是一些模板类的集合,不同之处就是容器中封装的是数据结构 1.序列容器 主要有vector向量容器.list列表容器.deque双端队列容器 元素在容器中是无序的 2.排序容器 包 ...

  2. STL学习:STL库vector、string、set、map用法

    本文仅介绍了如何使用它们常用的方法. vector 1.可随机访问,可在尾部插入元素:2.内存自动管理:3.头文件#include <vector> 1.创建vector对象 一维: (1 ...

  3. STL review:vector & string & map & struct

    I.vector 1.头文件:#include<vector>                        //容器vector是一个能实现随机存取.插入删除的动态数组,还可以当栈使. ...

  4. C++ 标准模板库(STL):vector

    目录 1. vector 1.1 vector的定义 1.2 vector容器内元素的访问 1.3 vector 常用函数实例解析 1.4 vector的常见用途 1. vector 变长数组,长度根 ...

  5. paper 5:支持向量机系列二: Support Vector —— 介绍支持向量机目标函数的 dual 优化推导,并得出“支持向量”的概念。

    paper 4中介绍了支持向量机,结果说到 Maximum Margin Classifier ,到最后都没有说“支持向量”到底是什么东西.不妨回忆一下上次最后一张图: 可以看到两个支撑着中间的 ga ...

  6. vector:动态数组

    vector是C++标准模板库中的部分内容,中文偶尔译作“容器”,但并不准确.它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存 ...

  7. Java集合系列(二):ArrayList、LinkedList、Vector的使用方法及区别

    本篇博客主要讲解List接口的三个实现类ArrayList.LinkedList.Vector的使用方法以及三者之间的区别. 1. ArrayList使用 ArrayList是List接口最常用的实现 ...

  8. 面试题:ArrayList、LinkedList、Vector三者的异同?

    面试题:ArrayList.LinkedList.Vector三者的异同? 同:三个类都是实现了List接口(Collection的子接口之一),存储数据的特点相同:存储有序的.可重复的数据不同: * ...

  9. STL顺序容器【vector】【deque】【list】

    我们都知道,stl在集装箱船分为两类,订购集装箱和相关的容器. 顺序容器有三种即动态数组vector,双端队列deque,以及链表list (对csdn的文字排版严重吐槽.写好的版发表了就变了) 一: ...

随机推荐

  1. bzoj5281/luogu4377 Talent Show (01分数规划+背包dp)

    就是01分数规划的思路,只不过当把w[i]-r*t[i]>0的选完以后如果w值还没达到要求,那就再01背包dp一下就好了(dp时w值>W的时候就存在W里就不会爆内存了). (跑得很慢..大 ...

  2. django xadmin

    1.11.13版本下的[安装]: 1.下载分支版本 https://github.com/nocmt/Xadmin1.11.x/archive/master.zip 2.解压,并将其放在site-pa ...

  3. SQL Server 增、删、改、小部分查

    --现有三个表Student.Score.Course.Teacher create table Student ( Sno ) not null,--学号 Sname ) not null,--姓名 ...

  4. js的append拼接html丢失css样式解决

    htmlApp += "<li id='leftli"+lunci+"'>"; htmlApp += "<span id='left ...

  5. 一个程序如何在调试时退出调试或退出while循环

    1.退出调试 按Ctrl+C 2.退出while循环 比如 #include <stdio.h> #include <stdlib.h> int main() { long a ...

  6. java8中的stream().filter()的使用和Optional()

    转: https://www.cnblogs.com/yimiyan/p/5992440.html Optional: https://www.cnblogs.com/zhangboyu/p/7580 ...

  7. elasticsearch-head安装及启动

    head是用于监控Elasticsearch状态的客户端插件,包括数据可视化,增删改查工具,es语句的可视化等等. 5.0之后的安装方式如下: git clone git://github.com/m ...

  8. .Net MVC 当前上下文中不存在名称“Style”

    遇到了这种问题,最后居然是我拼写错误了... 当然也有其他原因 http://blog.csdn.net/bianchao1/article/details/69666347 按照上面这篇博客的方法先 ...

  9. python---tornado初识(2)实现登录和发布文章

    # coding:utf8 # __author: Administrator # date: 2018/3/6 0006 # /usr/bin/env python import tornado.i ...

  10. Dapper总结(一)---基本CRUD操作

    一.dapper是什么 dapper是一款轻量级的ORM(Object Relationship Mapper),它负责数据库和编程语言之间的映射.SqlConnection,MysqlConnect ...