C++ std::vector
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
C++ std::vector的更多相关文章
- c++转载系列 std::vector模板库用法介绍
来源:http://blog.csdn.net/phoebin/article/details/3864590 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作 ...
- C++ 中的std::vector介绍(转)
vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...
- std::vector介绍
vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...
- std::vector<Channel2*> m_allChannels;容器,以及如何根据channelid的意义
std::vector<Channel2*> m_allChannels;容器,以及如何根据channelid的意义 这个容器保存了所有客户端连接的channel Channel2* Li ...
- std::vector数据复制
std::vector<boost::shared_ptr <ITEM> > srcItemList; // 数据源 std::vector<ITEM> des ...
- 单独删除std::vector <std::vector<string> > 的所有元素
下面为测试代码: 1.创建 std::vector< std::vector<string> > vc2; 2.初始化 std::vector<string> vc ...
- std::vector的分片拷贝和插入
一般我们在用Qt的QByteArrary或者List的时候,会有相应的append的方法,该函数,就是把数据加入末尾.但是std::vector就没有相应的方法.但是我们可以用insert方法来实现: ...
- 使用std::vector优化点云动画显示一例
1. 准备 使用std::vector应该知道几点: (1)内存连续的容器,有点像数组 (2)与std::list相比,插入和删除元素比较慢- 因为数据迁移 (3)添加元素可能会引发内存分配和数据迁移 ...
- 随机排序std::vector,扑克牌,麻将类尤其合用
有些需要重新对std::vector对象重新排序,特别是游戏,例如说:扑克牌,麻将,抽奖等,C++标准已经为std::vector写好了随机排序的方式,这里做个笔记: #include <alg ...
- (原创)动态内存管理练习 C++ std::vector<int> 模拟实现
今天看了primer C++的 “动态内存管理类”章节,里面的例子是模拟实现std::vector<std::string>的功能. 照抄之后发现编译不通过,有个库函数调用错误,就参考着自 ...
随机推荐
- Oracle分组函数实例
分组函数也叫聚合函数.如果在查询只想要查分组函数,那么跟平时的查询语句并无不同: SQL ,,,,) ; SUM(T.PRIZENUM) AVG(T.PRIZENUM) --------------- ...
- 将h264和aac码流合成flv文件
在视频应用中,经常需要将接收到h264和aac数据保存成文件. 本来想用mp4格式,但是mp4在没有正常关闭的情况下会导致文件打不开,而在实际应用中经常会出现设备直接拔电,程序不是正常结束的情况.于是 ...
- mysql索引之四:复合索引之最左前缀原理,索引选择性,索引优化策略之前缀索引
高效使用索引的首要条件是知道什么样的查询会使用到索引,这个问题和B+Tree中的“最左前缀原理”有关,下面通过例子说明最左前缀原理. 一.最左前缀索引 这里先说一下联合索引的概念.MySQL中的索引可 ...
- Java-Runoob-高级教程:Java 泛型
ylbtech-Java-Runoob-高级教程:Java 泛型 1.返回顶部 1. Java 泛型 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检 ...
- 手机页面开发的meta
<meta name="viewport" content="width=device-width, initial-scale=1"> 手机环境. ...
- 【BZOJ】1008: [HNOI2008]越狱(组合数学)
题目 题目描述 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 输入输出格式 ...
- Vue源码学习(零):内部原理解析
本篇文章是在阅读<剖析 Vue.js 内部运行机制>小册子后总结所得,想要了解详细内容,请参考原文:https://juejin.im/book/5a36661851882538e2259 ...
- docker中部署mongodb副本集
1.基本信息如下 服务器地址 192.168.73.129 副本集名称 rs 容器节点及端口映射 m0 37017:27017 m1 47017:27017 ...
- 用Jmeter制造软件测试数据
在测试过程难免会遇到一些需要大量测试数据的情况,如果数据没有太多的需求,或者需求本身比较简单,这时候我们可以用Jmeter参数化来实现(建议用badboy录制) 首先,我们可以通过badboy来录制实 ...
- Flask之模板之特殊变量和方法
3.6 Flask中的特殊变量和方法: 在Flask中,有一些特殊的变量和方法是可以在模板文件中直接访问的. config 对象: config 对象就是Flask的config对象,也就是 app. ...