std::vector

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

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.

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 (deques, lists 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.

Container properties

  • Sequence
  • Dynamic array: Allows direct 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.

Iterators:

begin,end,rbegin,rend,cbegin,cend,crbegin,crend

Capacity:

  • size:
  • max_size:
  • resize:
  • capacity: Return size of allocated storage capacity (public member function )
  • empty:
  • reserve: Request a change in capacity (public member function )
  • shrink_to_fit: Shrink to fit (public member function )

Element access:

  • operator[]:
  • at:
  • front: Access first element (public member function )
  • back: Access last element (public member function )
  • data: Access data (public member function )

Modifiers:

  • assign: Assign vector content (public member function )
  • push_back: Add element at the end (public member function )
  • pop_back: Delete last element (public member function )
  • insert
  • erase
  • swap
  • clear:
  • emplace: Construct and insert element (public member function )
  • emplace_back: Construct and insert element at the end (public member function )

Allocator:

  • get_allocator: Get allocator (public member function )

Non-member function overloads

  • relational operators Relational operators for vector (function template )
  • swap Exchange contents of vectors (function template )

Example

#include <iostream>
#include <vector> using namespace std; int main(int argc, char **argv)
{
vector<int> first1;
vector<int> first2(4,25); ///< Note:four ints with value 100
vector<int> first3(first2.begin(), first2.end());
vector<int> first4(first3); int intArr[] = {5,4,3,2,1};
vector<int> second(intArr, intArr + sizeof(intArr)/sizeof(int) ); cout << "vector first4: ";
for(auto it = first4.begin(); it != first4.end(); it++){
cout << *it << " ";
} cout << "\n";
return 0;
}

Reference

cplusplus


C++ 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. angularJS控制器之间的相互通信方式、$broadcast、$emit、$on

    在项目中,我们可能会很经常性的利用到控制器之间的相互通信,在angular中的控制器之间的相互通信有以下几种方式: 1)通过本地数据的存储localstorage,sessionstorage, 2) ...

  2. sqlplus连接的三种方式

    sys用户在cmd下以DBA身份登陆: sqlplus /nolog      --运行sqlplus命令,进入sqlplus环境.其中/nolog是不登陆到数据库服务器的意思,如果没有/nolog参 ...

  3. Java运算符,算术运算符

    算术运算符介绍 算术运算符用在数学表达式中,它们的作用和在数学中的作用一样. 下表列出了所有的算术运算符. 表格中的实例假设整数变量A的值为10,变量B的值为20: 操作符 描述 例子 + 加法 - ...

  4. POJ 3684 Physics Experiment(弹性碰撞)

    Physics Experiment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2936   Accepted: 104 ...

  5. Hive使用入门

    Hive简介 hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单的sql查询功能,可以将sql语句转换为MapReduce任务进行运行. 其优点是学习成 ...

  6. JeeSite导出多条数据(加复选框)demo

    表格图: jsp: 后台: @RequiresPermissions("shwindow:advertisementPutInList:view") @RequestMapping ...

  7. Python web框架 Tornado(三)自定义session组件

    我们在学习Django框架的过程中,内部封装了session组件,以方便于我们使用进行验证.但是Tornado框架是没有session的,所以如果想使用session的话,就需要我们自己定制相对应的组 ...

  8. URL传参时中文参数乱码的解决方法

    URL传参时,中文参数乱码的解决: 今天在工作中遇到了这样的一个问题,在页面之间跳转时,我将中文的参数放入到url中,使用location进行跳转传参,但是发现接收到的参数值是乱码.我的代码是这样写的 ...

  9. python中的socket模块

    熟悉了一下python的socket模块,感觉还是有点好玩的,不过坑也也是不少的. 1.服务器端代码 #!/usr/bin/env python import socket HOST='192.168 ...

  10. 基于AT UI实现表格的增删改查遇到的坑

    基于AT UI实现表格的增删改查遇到的坑 坑一.表格数据加载的渲染报错 报错:Error in render: "TypeError: Cannot read property 'isChe ...