Vector

Vectors are sequence containers representing arrays that can change in size.

Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

vector自动增长的原理:
Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the container.

Instead, vector containers may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the storage strictly needed to contain its elements (i.e., its size). Libraries can implement different strategies for growth to balance between memory usage and reallocations, but in any case, reallocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back).

Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.

Compared to the other dynamic sequence containers (dequeslists and forward_lists), vectors are very efficient accessing its elements (just like arrays) and relatively efficient adding or removing elements from its end. For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others, and have less consistent iterators and references than lists and forward_lists.

template < class T, class Alloc = allocator<T> > class vector; // generic template

Container properties

Sequence

  Elements in sequence containers are ordered in a strict linear sequence. Individual elements are accessed by their position in this sequence.

Dynamic array

  Allowsdirect access to any element in the sequence, even through pointer arithmetics, and provides relatively fast addition/removal of elements at the end of the sequence.

Allocator-aware

  The container uses an allocator object to dynamically handle its storage needs.

private:
  typedef _Vector_base<_Tp, _Alloc> _Base;
public:
  typedef _Tp value_type;
  typedef value_type* pointer;
  typedef const value_type* const_pointer;
  typedef value_type* iterator;
  typedef const value_type* const_iterator;
  typedef value_type& reference;
  typedef const value_type& const_reference;
  typedef size_t size_type;
  typedef ptrdiff_t difference_type;
 
从头文件中可以看出,iterator实质就是一个指针。
 
vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays.
 
vector<int> ivenc;
for (vector<int>::iterator iter = ivenc.begin(); iter != ivenc.end(); ++iter)
{
*iter = ;
}

续:对vector的操作

std::vector的更多相关文章

  1. c++转载系列 std::vector模板库用法介绍

    来源:http://blog.csdn.net/phoebin/article/details/3864590 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作 ...

  2. C++ 中的std::vector介绍(转)

    vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...

  3. std::vector介绍

    vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...

  4. std::vector<Channel2*> m_allChannels;容器,以及如何根据channelid的意义

    std::vector<Channel2*> m_allChannels;容器,以及如何根据channelid的意义 这个容器保存了所有客户端连接的channel Channel2* Li ...

  5. std::vector数据复制

    std::vector<boost::shared_ptr <ITEM> > srcItemList;  // 数据源 std::vector<ITEM>  des ...

  6. 单独删除std::vector <std::vector<string> > 的所有元素

    下面为测试代码: 1.创建 std::vector< std::vector<string> > vc2; 2.初始化 std::vector<string> vc ...

  7. std::vector的分片拷贝和插入

    一般我们在用Qt的QByteArrary或者List的时候,会有相应的append的方法,该函数,就是把数据加入末尾.但是std::vector就没有相应的方法.但是我们可以用insert方法来实现: ...

  8. 使用std::vector优化点云动画显示一例

    1. 准备 使用std::vector应该知道几点: (1)内存连续的容器,有点像数组 (2)与std::list相比,插入和删除元素比较慢- 因为数据迁移 (3)添加元素可能会引发内存分配和数据迁移 ...

  9. 随机排序std::vector,扑克牌,麻将类尤其合用

    有些需要重新对std::vector对象重新排序,特别是游戏,例如说:扑克牌,麻将,抽奖等,C++标准已经为std::vector写好了随机排序的方式,这里做个笔记: #include <alg ...

  10. (原创)动态内存管理练习 C++ std::vector<int> 模拟实现

    今天看了primer C++的 “动态内存管理类”章节,里面的例子是模拟实现std::vector<std::string>的功能. 照抄之后发现编译不通过,有个库函数调用错误,就参考着自 ...

随机推荐

  1. [CF98E]Help Shrek and Donkey

    题意:A和B两个卡牌大师玩游戏,A有$n$张牌,B有$m$张牌,桌上有$1$张牌,这$n+m+1$张牌互不相同且A和B都知道这些牌里有什么牌(但他们互相不知道对方有什么牌,两个人也都不知道桌上的那张牌 ...

  2. 【kmp算法】hdu4763 Theme Section

    kmp中next数组的含义是:next[i]表示对于s[0]~s[i-1]这个前缀而言,最大相等的前后缀的长度是多少.规定next[0]=-1. 迭代for(int i=next[i];i!=-1;i ...

  3. 【二分】【动态规划】Codeforces Round #393 (Div. 1) B. Travel Card

    水dp,加个二分就行,自己看代码. B. Travel Card time limit per test 2 seconds memory limit per test 256 megabytes i ...

  4. JavaBean的详细及引用

    1.JavaBean实际是具有统一接口格式的java类 2.JavaBean的组成:属性(Properties).方法(Method).事件(Events) 3.一个JavaBean的例子(该例子是用 ...

  5. RequireJS 2.0初探

    就在前天晚上RequireJS发布了一个大版本,直接从version1.0.8升级到了2.0.随后的几小时James Burke又迅速的将版本调整为2.0.1,当然其配套的打包压缩工具r.js也同时升 ...

  6. 【mybatis】mybatis方法访问报错:org.apache.ibatis.builder.IncompleteElementException: Could not find result map com.pisen.cloud.luna.ms.goods.base.domain.GoodsConfigQuery

    在调用mapper.xml中的方法的时候,报错: org.apache.ibatis.builder.IncompleteElementException: Could not find result ...

  7. DOM系统学习-进阶

    DOM类型  Node类型: 常用类型: ​    ​    ​元素节点 ELEMENT_NODE ​    ​    ​属性节点 ATTRIBUTE_NODE ​    ​    ​文本节点 TEX ...

  8. 特征选取方法PCA与LDA

    一.主成分分析(PCA)介绍 什么是主成分分析?   主成分分析是一种用于连续属性降维的方法,把多指标转化为少数几个综合指标. 它构造了原始属性的一个正交变换,将一组可能相关的变量转化为一组不相关的变 ...

  9. 第三天 ThinkPHP手把手高速拼接站点(三)

    6月1日,小雨." 梅子金黄杏子肥,麦花雪白菜花稀. 日长篱落无人过,唯有蜻蜓蛱蝶飞." 七.MVC模式 ThinkPHP的MVC开发机制例如以下: M  Model层    模型 ...

  10. Linux GPRS模块问题

    这是一个硬件问题,不过被我这个学软件的给遇到了.很尴尬,纠结了很久. GPRS模块如果没有插上sim卡,开机之后一切正常.一旦插上卡之后开机大约过十秒钟之后会自动关机.并在串口上面打印一下信息: II ...