std::vector
来自cppreference.com
Defined in header <vector>
|
||
template< class T, |
||
std::vector
是一种序列容器,是对大小可变数组封装。
数组中的元素是连续存储的,所以除了能够通过迭代器访问外,还可以通过常规的指针偏移量访问元素。换句话说,可以将指向 vector 元素的指针传入以指向数组元素的指针作为参数的函数。
vector 会在需要时自动调整所占内存的大小。与对应的静态数组相比,vector 所占的内存通常要更多,因为它还分配了额外的内存以应对将来可能的扩张。于是,vector 就不必在每次插入元素时都重新分配一次内存了,除非这块预留的内存用尽。已分配内存的总大小可以通过 capacity()
函数查询。所占的额外的内存可以通过调用 shrink_to_fit()
返还给系统。
从性能方面考虑,内存重分配操作的代价通常很大。如果事先知道元素个数,可以使用 reserve()
函数消除重新分配操作。
针对 vector 的各种常见操作的复杂度(效率)如下:
- 随机访问 - 常数 O(1)
- 在尾部增删元素 - 摊余(amortized)常数 O(1)}}
- 增删元素 - 至 vector 尾部的线性距离 O(n)}}
std::vector
满足 Container
、AllocatorAwareContainer
、SequenceContainer
和 ReversibleContainer
的要求。
目录 |
[编辑] 特化
标准库为 bool 类型提供了特化版本的 std::vector
,优化了空间利用率。
节省空间的动态bitset的 Original: space-efficient dynamic bitset The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (类模板) |
[编辑] 成员类型
成员类型 | 定义 |
value_type
|
T
|
allocator_type
|
Allocator
|
size_type
|
无符号整数类型(通常是size_t)
Original: Unsigned integral type (usually size_t) The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
difference_type
|
Signed integer type (usually ptrdiff_t) |
reference
|
Allocator::reference (至 C++11)value_type& (C++11 起)
|
const_reference
|
Allocator::const_reference (至 C++11)const value_type& (C++11 起)
|
pointer
|
Allocator::pointer (至 C++11)std::allocator_traits<Allocator>::pointer (C++11 起) |
const_pointer
|
Allocator::const_pointer (至 C++11) std::allocator_traits<Allocator>::const_pointer (C++11 起) |
iterator
|
RandomAccessIterator
|
const_iterator
|
恒随机访问迭代器
Original: Constant random access iterator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
reverse_iterator
|
std::reverse_iterator<iterator> |
const_reverse_iterator
|
std::reverse_iterator<const_iterator> |
[编辑] 成员函数
构建 vector Original: constructs the vector The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
destructs the vector (公共成员函数) | |
将值分配到容器中 Original: assigns values to the container The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
将值分配到容器中 Original: assigns values to the container The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
返回的关联分配器 Original: returns the associated allocator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
元素访问 | |
访问指定的元素与边界检查 Original: access specified element with bounds checking The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
访问指定的元素 Original: access specified element The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
访问的第一个元素 Original: access the first element The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
access the last element (公共成员函数) | |
(C++11) |
直接访问底层数组 Original: direct access to the underlying array The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) |
迭代器 | |
返回一个迭代的开始 Original: returns an iterator to the beginning The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
返回一个迭代的结束 Original: returns an iterator to the end The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
返回一个反向迭代的开始 Original: returns a reverse iterator to the beginning The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
返回一个反向迭代结束 Original: returns a reverse iterator to the end The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
容量 | |
检查是否容器是空的 Original: checks whether the container is empty The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
返回的元素数 Original: returns the number of elements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
返回可能的最大数量的元素 Original: returns the maximum possible number of elements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
储备存储 Original: reserves storage The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
返回的元素的数目,可以保持在当前分配的存储空间 Original: returns the number of elements that can be held in currently allocated storage The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
(C++11) |
通过释放未使用的内存减少了内存的使用情况 Original: reduces memory usage by freeing unused memory The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) |
Original: Modifiers The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
清除其内容 Original: clears the contents The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
插入元素 Original: inserts elements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
(C++11) |
constructs element in-place (公共成员函数) |
擦除元素 Original: erases elements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
将元素添加到年底 Original: adds elements to the end The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
(C++11) |
构造就地中的元素,在末端 Original: constructs elements in-place at the end The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) |
删除最后一个元素 Original: removes the last element The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
changes the number of elements stored (公共成员函数) | |
交换的内容 Original: swaps the contents The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) |
[编辑] 非成员函数
字典中的值比较的vector Original: lexicographically compares the values in the vector The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
专业的std::swap算法 Original: specializes the std::swap algorithm The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) |