vector 向量(动态数组)
头文件
#include <vector>
定义
template <class T, class Allocator = std::allocator<T>>
class vector;
成员类型
| 成员类型 | 定义 |
|---|---|
| value_type | T |
| allocator_type | Allocator |
| size_type | 无符号整数类型(通常为 std::size_t) |
| reference | value_type& |
| const_reference | const value_type& |
成员函数
构造、析构与赋值
构造函数
vector(); // 默认构造函数。构造一个空的 vector,使用默认的分配器。
// 示例:std::vector<int> vec; // 创建一个空的 vector
explicit vector(const Allocator& alloc); // 构造一个空的 vector,使用给定的分配器 alloc
// 示例:std::allocator<int> alloc; std::vector<int> vec(alloc);
explicit vector(size_type count, const Allocator& alloc = Allocator()); // 构造一个包含 count 个默认插入的 T 对象的 vector
// 示例:std::vector<int> vec(5); // vec = {0, 0, 0, 0, 0}
vector(size_type count, const T& value, const Allocator& alloc = Allocator()); // 构造一个包含 count 个值为 value 的元素的副本的 vector
// 示例:std::vector<int> vec(5, 666); // vec = {666, 666, 666, 666, 666}
template<class InputIt>
vector(InputIt first, InputIt last, const Allocator& alloc = Allocator()); // 构造一个包含范围 [first, last) 内容的 vector
// 示例:int arr[] = {1, 2, 3, 4, 5}; std::vector<int> vec(arr, arr + 3); // vec = {1, 2, 3}
// 示例:std::vector<int> other = {1, 2, 3, 4, 5}; std::vector<int> vec(other.begin(), other.begin() + 4); // vec = {1, 2, 3, 4}
vector(vector&& other); // 移动构造函数。从 other 移动构造内容
// 示例:std::vector<int> other = {1, 2, 3}; std::vector<int> vec = std::move(other); // vec = {1, 2, 3}, other = {}
vector(const vector& other); // 拷贝构造函数
// 示例:std::vector<int> other = {1, 2, 3}; std::vector<int> vec(other); // vec = {1, 2, 3}
vector(const vector& other, const Allocator& alloc); // 带分配器的拷贝构造函数,使用 alloc 作为分配器
// 示例:std::vector<int> other = {1, 2, 3}; std::allocator<int> alloc; std::vector<int> vec(other, alloc);
vector(vector&& other, const Allocator& alloc); // 带分配器的移动构造函数,使用 alloc 作为分配器
// 示例:std::vector<int> other = {1, 2, 3}; std::allocator<int> alloc; std::vector<int> vec(std::move(other), alloc);
vector(std::initializer_list<T> init, const Allocator& alloc = Allocator()); // 使用初始化列表构造 vector
// 示例:std::vector<int> vec = {1, 2, 3}; // vec = {1, 2, 3}
析构函数
~vector(); // 销毁 vector。元素的析构函数被调用,并释放所使用的存储空间。请注意,如果元素是指针,则指向的对象不会被销毁。
operator= : 将值赋给容器
vector& operator=(const vector& other); // 复制赋值运算符。用 other 内容的副本替换内容
// 示例:std::vector<int> vec = {1, 2, 3}; std::vector<int> other = {4, 5, 6}; vec = other; // vec = {4, 5, 6}
vector& operator=(vector&& other); // 移动赋值运算符。使用移动语义替换内容。之后,other 处于有效但未指定的状态。
// 示例:std::vector<int> vec = {1, 2, 3}; std::vector<int> other = {4, 5, 6}; vec = std::move(other); // vec = {4, 5, 6}
vector& operator=(std::initializer_list<value_type> ilist); // 用初始化列表 ilist 标识的内容替换内容
// 示例:std::vector<int> vec = {1, 2, 3}; vec = {4, 5, 6}; // vec = {4, 5, 6}
assign : 将值赋给容器
void assign(size_type count, const T& value); // 将内容替换为 count 个 value 的副本
// 示例:std::vector<int> vec = {1, 2, 3}; vec.assign(5, 666); // vec = {666, 666, 666, 666, 666}
template<class InputIt>
void assign(InputIt first, InputIt last); // 用范围 [first, last) 中的元素副本替换内容
// 示例:std::vector<int> vec = {1, 2, 3}; std::vector<int> other = {4, 5, 6}; vec.assign(other.begin(), other.end()); // vec = {4, 5, 6}
void assign(std::initializer_list<T> ilist); // 将内容替换为 ilist 中的元素
// 示例:std::vector<int> vec = {1, 2, 3}; vec.assign({4, 5, 6}); // vec = {4, 5, 6}
get_allocator : 返回关联的分配器
allocator_type get_allocator() const; // 返回与容器关联的分配器
// 示例:std::vector<int> vec; std::allocator<int> alloc = vec.get_allocator();
assign_range (C++23) : 将一个范围的值赋给容器
template<container-compatible-range<T> R>
constexpr void assign_range(R&& rg); // 用 rg 中每个元素的副本替换容器中的元素
// 示例:std::vector<int> vec = {1, 2, 3}; std::vector<int> other = {4, 5, 6}; vec.assign_range(other); // vec = {4, 5, 6}
元素访问
operator[] : 访问指定的元素
reference operator[](size_type pos); // 返回对指定位置 pos 元素的引用。不执行边界检查。
// 示例:std::vector<int> vec = {1, 2, 3}; vec[1] = 666; // vec = {1, 666, 3}
const_reference operator[](size_type pos) const; // 返回对 const vector 指定位置 pos 元素的常引用。不执行边界检查。
// 示例:const std::vector<int> vec = {1, 2, 3}; int value = vec[0]; // value = 1
at : 访问指定的元素,带边界检查
reference at(size_type pos); // 返回指定位置 pos 元素的引用,并进行边界检查。如果 pos 不在容器的范围内,则抛出类型为 std::out_of_range 的异常。
// 示例:std::vector<int> vec = {1, 2, 3}; try { vec.at(10) = 999; } catch(const std::out_of_range& e) { /* handle error */ }
const_reference at(size_type pos) const; // 返回 const vector 指定位置 pos 元素的常引用,并进行边界检查。
// 示例:const std::vector<int> vec = {1, 2, 3}; int value = vec.at(0); // value = 1
front : 访问第一个元素
reference front(); // 返回 vector 中第一个元素的引用。等价于 *begin()。
// 示例:std::vector<int> vec = {1, 2, 3}; vec.front() = 666; // vec = {666, 2, 3}
const_reference front() const; // 返回 const vector 中第一个元素的常引用。等价于 *cbegin()。
// 示例:const std::vector<int> vec = {1, 2, 3}; int value = vec.front(); // value = 1
back : 访问最后一个元素
reference back(); // 返回 vector 中最后一个元素的引用。
// 示例:std::vector<int> vec = {1, 2, 3}; vec.back() = 666; // vec = {1, 2, 666}
const_reference back() const; // 返回 const vector 中最后一个元素的常引用。
// 示例:const std::vector<int> vec = {1, 2, 3}; int value = vec.back(); // value = 3
data : 直接访问底层连续存储
T* data(); // 返回指向 vector 中作为元素存储的底层数组的指针。
// 示例:std::vector<int> vec = {1, 2, 3}; int* ptr = vec.data(); // ptr 指向 vec 内部数组的首元素
const T* data() const; // 返回指向 const vector 中作为元素存储的底层数组的常量指针。
// 示例:const std::vector<int> vec = {1, 2, 3}; const int* ptr = vec.data();
迭代器
begin, cbegin (C++11) : 返回指向起始的迭代器
iterator begin(); // 返回指向 vector 第一个元素的迭代器。如果 vector 为空,则返回的迭代器将等于 end()。
// 示例:std::vector<int> vec = {1, 2, 3}; std::vector<int>::iterator it = vec.begin();
const_iterator begin() const; // 返回指向 vector 第一个元素的常量迭代器。
// 示例:const std::vector<int> vec = {1, 2, 3}; std::vector<int>::const_iterator it = vec.begin();
const_iterator cbegin() const noexcept; // 返回指向 vector 第一个元素的常量迭代器。
// 示例:std::vector<int> vec = {1, 2, 3}; std::vector<int>::const_iterator it = vec.cbegin();
end, cend (C++11) : 返回指向末尾的迭代器
iterator end() noexcept; // 返回一个迭代器,指向 vector 中最后一个元素的后一个位置。
// 示例:std::vector<int> vec = {1, 2, 3}; std::vector<int>::iterator it = vec.end();
const_iterator end() const noexcept; // 返回一个常量迭代器,指向 vector 中最后一个元素的后一个位置。
// 示例:const std::vector<int> vec = {1, 2, 3}; std::vector<int>::const_iterator it = vec.end();
const_iterator cend() const noexcept; // 返回一个常量迭代器,指向 vector 中最后一个元素的后一个位置。
// 示例:std::vector<int> vec = {1, 2, 3}; std::vector<int>::const_iterator it = vec.cend();
rbegin, crbegin (C++11) : 返回指向起始的逆向迭代器
reverse_iterator rbegin(); // 返回一个逆向迭代器,指向反向 vector 的第一个元素。它对应于非反向 vector 的最后一个元素。
// 示例:std::vector<int> vec = {1, 2, 3}; std::vector<int>::reverse_iterator it = vec.rbegin();
const_reverse_iterator rbegin() const; // 返回一个逆向常量迭代器,指向反向 vector 的第一个元素。
// 示例:const std::vector<int> vec = {1, 2, 3}; std::vector<int>::const_reverse_iterator it = vec.rbegin();
const_reverse_iterator crbegin() const noexcept; // 返回一个逆向常量迭代器,指向反向 vector 的第一个元素。
// 示例:std::vector<int> vec = {1, 2, 3}; std::vector<int>::const_reverse_iterator it = vec.crbegin();
rend, crend (C++11) : 返回指向末尾的逆向迭代器
reverse_iterator rend(); // 返回一个逆向迭代器,指向反转 vector 的最后一个元素之后的元素。
// 示例:std::vector<int> vec = {1, 2, 3}; std::vector<int>::reverse_iterator it = vec.rend();
const_reverse_iterator rend() const; // 返回一个逆向常量迭代器,指向反转 vector 的最后一个元素之后的元素。
// 示例:const std::vector<int> vec = {1, 2, 3}; std::vector<int>::const_reverse_iterator it = vec.rend();
const_reverse_iterator crend() const noexcept; // 返回一个逆向常量迭代器,指向反转 vector 的最后一个元素之后的元素。
// 示例:std::vector<int> vec = {1, 2, 3}; std::vector<int>::const_reverse_iterator it = vec.crend();
容量
size : 返回元素数量
size_type size() const; // 返回容器中的元素数量,即 std::distance(begin(), end())。
// 示例:std::vector<int> vec = {1, 2, 3}; std::cout << vec.size(); // 输出 : 3
empty : 检查容器是否为空
bool empty() const; // 检查容器是否没有元素,即 begin() == end() 是否为真。
// 示例:std::vector<int> vec; std::cout << vec.empty(); // 输出 : 1
capacity : 返回当前已分配存储空间中可容纳的元素数量
size_type capacity() const; // 返回容器当前已分配空间所能容纳的元素数量。
// 示例:std::vector<int> vec = {1, 2, 3}; std::cout << vec.capacity(); // 输出 : 3(或更大)
reserve : 预留存储空间
void reserve(size_type new_cap); // 将 vector 的容量增加到大于或等于 new_cap 的值。reserve() 不会改变 vector 的大小。
// 示例:std::vector<int> vec; vec.reserve(5); std::cout << vec.capacity(); // 输出 : 5
shrink_to_fit : 通过释放未使用的内存来减少内存使用
void shrink_to_fit(); // 请求移除未使用的容量。这是一个非强制性的请求,要求将 capacity() 减小到 size()。
// 示例:std::vector<int> vec = {1, 2, 3}; vec.reserve(10); vec.shrink_to_fit();
max_size : 返回元素的最大可能数量
size_type max_size() const; // 返回容器能够容纳的最大元素数量,受系统或库实现限制。
// 示例:std::vector<int> vec; std::cout << vec.max_size();
修改器
push_back : 添加元素到结尾
void push_back(const T& value); // 将给定元素 value 添加到容器末尾。新元素初始化为 value 的副本。
// 示例:std::vector<int> vec = {1, 2, 3}; vec.push_back(666); // vec = {1, 2, 3, 666}
void push_back(T&& value); // 将给定元素 value 添加到容器末尾。将 value 移动到新元素中。
// 示例:std::vector<std::string> vec = {"Hello"}; std::vector<std::string> other = {"World"}; vec.push_back(std::move(other[0]));
pop_back : 移除末元素
void pop_back(); // 移除容器的最后一个元素。在空容器上调用 pop_back 会导致未定义行为。
// 示例:std::vector<int> vec = {1, 2, 3}; vec.pop_back(); // vec = {1, 2}
resize : 更改存储的元素数量
void resize(size_type count); // 将容器的大小调整为包含 count 个元素。如果当前大小小于 count,则追加额外的默认插入元素。
// 示例:std::vector<int> vec = {1, 2, 3}; vec.resize(5); // vec = {1, 2, 3, 0, 0}
void resize(size_type count, const value_type& value); // 将容器的大小调整为包含 count 个元素。追加 value 的额外副本。
// 示例:std::vector<int> vec = {1, 2, 3}; vec.resize(5, 666); // vec = {1, 2, 3, 666, 666}
insert : 插入元素
iterator insert(const_iterator pos, const T& value); // 在容器的指定位置插入元素。在 pos 前插入 value 的副本。
// 示例:std::vector<int> vec = {1, 2, 3}; vec.insert(vec.begin() + 1, 666); // vec = {1, 666, 2, 3}
iterator insert(const_iterator pos, T&& value); // 在容器的指定位置插入元素。在 pos 前插入 value,可能使用移动语义。
// 示例:std::vector<std::string> vec = {"Hello"}; std::vector<std::string> other = {"World"}; vec.insert(vec.end(), std::move(other[0]));
iterator insert(const_iterator pos, size_type count, const T& value); // 在容器的指定位置插入元素。在 pos 前插入 count 个 value 的副本。
// 示例:std::vector<int> vec = {1, 2, 3}; vec.insert(vec.begin() + 1, 3, 666); // vec = {1, 666, 666, 666, 2, 3}
template<class InputIt>
iterator insert(const_iterator pos, InputIt first, InputIt last); // 在容器的指定位置插入元素。在 pos 前插入来自范围 [first, last) 的元素。
// 示例:std::vector<int> vec = {1, 2, 3}; std::vector<int> other = {4, 5, 6}; vec.insert(vec.begin() + 1, other.begin(), other.end());
iterator insert(const_iterator pos, std::initializer_list<T> ilist); // 在容器的指定位置插入元素。在 pos 前插入来自初始化列表 ilist 的元素。
// 示例:std::vector<int> vec = {1, 2, 3}; vec.insert(vec.begin() + 1, {4, 5, 6});
erase : 擦除元素
iterator erase(iterator pos); // 从容器中擦除指定元素。移除 pos 处的元素。
// 示例:std::vector<int> vec = {1, 2, 3}; vec.erase(vec.begin() + 1); // vec = {1, 3}
iterator erase(const_iterator pos); // 从容器中擦除指定元素。移除 pos 处的元素。
// 示例:std::vector<int> vec = {1, 2, 3}; vec.erase(vec.cbegin()); // vec = {2, 3}
iterator erase(iterator first, iterator last); // 从容器中擦除指定元素。移除范围 [first, last) 中的元素。
// 示例:std::vector<int> vec = {1, 2, 3, 4, 5}; vec.erase(vec.begin() + 1, vec.begin() + 4); // vec = {1, 5}
iterator erase(const_iterator first, const_iterator last); // 从容器中擦除指定元素。移除范围 [first, last) 中的元素。
// 示例:std::vector<int> vec = {1, 2, 3, 4, 5}; vec.erase(vec.cbegin() + 2, vec.cend()); // vec = {1, 2}
clear : 清除内容
void clear(); // 从容器中擦除所有元素。在此调用之后,size() 返回零。使任何指向所含元素的引用、指针和迭代器失效。
// 示例:std::vector<int> vec = {1, 2, 3}; vec.clear(); // vec = {}
swap : 交换内容
void swap(vector& other); // 与 other 交换容器的内容和容量。不调用单个元素的任何移动、复制或交换操作。
// 示例:std::vector<int> vec = {1, 2, 3}; std::vector<int> other = {4, 5, 6}; vec.swap(other); // vec = {4, 5, 6}, other = {1, 2, 3}
emplace (C++11) : 就地构造元素
template<class... Args>
iterator emplace(const_iterator pos, Args&&... args); // 在 pos 之前直接构造一个新元素。
// 示例:struct Point { Point(int x, int y) : x(x), y(y) {} int x, y; }; std::vector<Point> vec; vec.emplace(vec.begin(), 1, 2);
emplace_back (C++11) : 就地构造元素于结尾
template<class... Args>
void emplace_back(Args&&... args); // 在容器末尾直接构造一个新元素。
// 示例:struct Point { Point(int x, int y) : x(x), y(y) {} int x, y; }; std::vector<Point> vec; vec.emplace_back(1, 2);
insert_range (C++23) : 插入元素范围
template<container-compatible-range<T> R>
constexpr iterator insert_range(const_iterator pos, R&& rg); // 在 pos 之前,以非逆序插入 rg 中元素的副本。
// 示例:std::vector<int> vec = {1, 2, 3}; std::vector<int> other = {4, 5, 6}; vec.insert_range(vec.begin() + 1, other);
append_range (C++23) : 添加一个元素范围到结尾
template<container-compatible-range<T> R>
constexpr void append_range(R&& rg); // 在 end() 之前,以非逆序插入来自范围 rg 的元素副本。
// 示例:std::vector<int> vec = {1, 2, 3}; std::vector<int> other = {4, 5, 6}; vec.append_range(other);
