https://en.cppreference.com/w/cpp/container
- 头文件
<vector>
- 声明
1)
template<
class T,
class Allocator = std::allocator<T>
> class vector;
2)从C++17开始是
namespace pmr {
template <class T>
using vector = std::vector<T, std::pmr::polymorphic_allocator<T>>;
}
说明:
1) std::vector是一个顺序容器,它封装了动态大小的数组。
2) std::pmr::vector 是使用“ polymorphic allocator”的一个模板别称。
- 描述
vector内的所有元素是内存连续存储的,也就是说,这些元素不仅可以通过迭代器访问,也可以普通的指针偏移来访问。这意味着,一个vector内指向一个元素的指针可以作为参数被传递到任何需要这个数组的函数。
从C++03
如果需要扩展或连接,vector的存储空间是自动处理的。vector通常要比静态数组占用更多的内存空间,因为需要分配更多的内存来处理以后可能的增长;这样,在进行insert操作时,不需要每次重分配内存,只有当现有的内存空间没有剩余时才进行重分配。整个已分配的内存的大小可以通过使用capacity()函数来获取。可以通过调用 shrink_to_fit() 来获取额外的内存。
从C++11
因为重分配是非常耗时的操作。 如果我们已知数组内最大元素个数时,reserve()函数可以用于消除重分配。
下面是vector的操作的复杂度描述:
1)随机访问--固定 O(1)
2)在尾部插入或删除元素---amortized constant O(1)
3)插入或删除到尾部固定距离的元素----O(n)
std::vector (for T other than bool) meets the requirements of Container, AllocatorAwareContainer, SequenceContainer , ContiguousContainer (since C++17) and ReversibleContainer.
- 模板参数
T | The type of the elements. The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type meets the requirements of Erasable, but many member functions impose stricter requirements. This container (but not its members) can be instantiated with an incomplete element type if the allocator satisfies the allocator completeness requirements. (since C++17) |
Allocator | An allocator that is used to acquire/release memory and to construct/destroy the elements in that memory. The type must meet the requirements of Allocator. The behavior is undefined if Allocator::value_type is not the same as T. |
- 特例--bool类型
The standard library provides a specialization of std::vector for the type bool, which may be optimized for space efficiency.
vector<bool> space-efficient dynamic bitset (class template specialization)
- 迭代器操作和限制
操作 | 非法情况 |
所有的读操作 | 无非法情况 |
swap, std::swap | end() |
clear, operator=, assign | 所有都非法 |
reserve, shrink_to_fit | If the vector changed capacity, all of them. If not, none. |
erase | Erased elements and all elements after them (including end()) |
push_back, emplace_back | If the vector changed capacity, all of them. If not, none. |
insert, emplace | If the vector changed capacity, all of them. If not, only those at or after the insertion point (including end()). |
resize | If the vector changed capacity, all of them. If not, only end() and any elements erased. |
pop_back | The element erased and end(). |
- 成员类型
成员类型 | 定义 |
value_type | T |
allocator_type | Allocator |
size_type | Unsigned integer type (usually std::size_t) |
difference_type | Signed integer type (usually std::ptrdiff_t) |
reference | Allocator::reference (until C++11) value_type& (since C++11) |
const_reference | Allocator::const_reference (until C++11) const value_type& (since C++11) |
pointer | Allocator::pointer (until C++11) std::allocator_traits<Allocator>::pointer (since C++11) |
const_pointer | Allocator::const_pointer (until C++11) std::allocator_traits<Allocator>::const_pointer (since C++11) |
iterator | LegacyRandomAccessIterator |
const_iterator | Constant LegacyRandomAccessIterator |
reverse_iterator | std::reverse_iterator<iterator> |
const_reverse_iterator | std::reverse_iterator<const_iterator> |
- 元素访问
at | access specified element with bounds checking (public member function) |
operator[] | access specified element (public member function) |
front | access the first element (public member function) |
back | access the last element (public member function) |
data | (C++11) direct access to the underlying array (public member function) |
- 迭代器操作
begin cbegin | (C++11)returns an iterator to the beginning (public member function) |
rbegin crbegin | (C++11)rreturns a reverse iterator to the beginning (public member function) |
end cend | (C++11)returns an iterator to the end (public member function) |
rend crend | (C++11)returns a reverse iterator to the end (public member function) |
- 获取能力指标
empty | checks whether the container is empty (public member function) |
size | returns the number of elements (public member function) |
max_size | returns the maximum possible number of elements (public member function) |
reserve | reserves storage (public member function) |
capacity | returns the number of elements that can be held in currently allocated storage (public member function) |
shrink_to_fit | (C++11)reduces memory usage by freeing unused memory (public member function) |
- 修改操作
clear | void clear();(until C++11) void clear() noexcept;(since C++11) | clears the contents (public member function) |
insert | 1)(until C++11) iterator insert( iterator pos, const T& value ); (since C++11)iterator insert( const_iterator pos, const T& value ); | inserts elements (public member function) |
emplace | template< class... Args > iterator emplace( const_iterator pos, Args&&... args );(since C++11) | (C++11)constructs element in-place (public member function) |
erase | (1) (until C++11)iterator erase( iterator pos ); (since C++11)iterator erase( const_iterator pos ); (2) (until C++11)iterator erase( iterator first, iterator last ); (since C++11)iterator erase( const_iterator first, const_iterator last ); | erases elements (public member function) |
push_back | (1) void push_back( const T& value ); (2) (since C++11)void push_back( T&& value ); | adds an element to the end (public member function) |
emplace_back | (since C++11)(until C++17) template< class... Args > | (C++11)constructs an element in-place at the end (public member function) |
pop_back | void pop_back(); | removes the last element (public member function) |
resize | (until C++11) void resize( size_type count, T value = T() );
(since C++11) 1)void resize( size_type count ); | changes the number of elements stored (public member function) |
swap | (until C++17)void swap( vector& other ); (since C++17)(until C++20)void swap( vector& other ) noexcept(/* see below */); (since C++20)constexpr void swap( vector& other ) noexcept(/* see below */); | swaps the contents (public member function) |
- 举例
#include <iostream>
#include <vector>
int main()
{
// Create a vector containing integers
std::vector<int> v = {7, 5, 16, 8};
// Add two more integers to vector
v.push_back(25);
v.push_back(13);
// Iterate and print values of vector
for(int n : v) {
std::cout << n << '\n';
}
}
Output:
7
5
16
8
25
13