STL——vector
来源:互联网 发布:电信 网络重构 编辑:程序博客网 时间:2024/04/30 17:44
参考自CPP标准文档vector
一、定义
Vectors are sequence containers representing arrays that can change in size
特点:
1. Sequence
Elements in sequence containers are ordered in a strict linear sequence. Individual elements are accessed by their position in this sequence.
2. 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.
3. Allocator-aware
The container uses an allocator object to dynamically handle its storage needs
初始化
// constructors used in the same order as described above: std::vector<int> first; // empty vector of ints std::vector<int> second (4,100); // four ints with value 100 std::vector<int> third (second.begin(),second.end()); // iterating through second std::vector<int> fourth (third); // a copy of third // the iterator constructor can also be used to construct from arrays: int myints[] = {16,2,77,29}; std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
vector 的data成员,返回指向数组的首地址,因为vector是线性连续存储的,所以可以通过指针的偏移访问vector的元素。
#include <iostream>#include <vector>int main (){ std::vector<int> myvector (5); int* p = myvector.data(); *p = 10; ++p; *p = 20; p[2] = 100; std::cout << "myvector contains:"; for (unsigned i=0; i<myvector.size(); ++i) std::cout << ' ' << myvector[i]; std::cout << '\n'; return 0;}
Output:
myvector contains: 10 20 0 100 0
swap
vector的交换大小可以不一样,同array不同(array的size必须一样才可以交换)
使用区别:
1、如果你需要高效的随机存取(即[]操作),不在乎插入和删除的效率,应该使用vector
2. 如果你需要大量的插入和删除,不关心随机存取,应该使用list
3. 如果你需要随机存取,而且关心数据的插入删除,应该使用deque
- STL — — vector
- STL——vector
- STL——vector
- STL—vector
- STL ——vector 学习
- STL——Vector Test
- STL总结——vector
- STL—vector容器类型
- STL源码解析—vector
- STL源码解析—vector
- STL — vector源代码剖析
- vector向量容器——STL Introduction
- STL源码剖析 — — vector
- STL源码剖析——vector
- 回归季——C++ STL vector
- STL 源码学习笔记——vector
- stl之序列容器——vector
- STL学习笔记— —容器vector
- 如何在windows server2012中查看已安装的证书
- 医院污物真空管道收集系统综述报告
- 流程虚拟机——jBPM产品思想分析.pdf
- 修改Eclipse/MyEclipse项目的默认编码
- 扩展KMP+KMP+hdu4333
- STL——vector
- Training--使用Fragment创建动态UI
- 内核工具 – Sparse 简介,:__attribute, __context__
- jQuery 学习(三)
- Given an array of integers, every element appears twice except for one. Find that single one.
- bzoj1196: [HNOI2006]公路修建问题 二分答案
- Eureka 的 Application Client 客户端的运行示例
- 什么是艺术--草论
- 针对android方法数64k的限制,square做出的努力。精简protobuf