vector 向量(动态数组)
头文件
#include <vector>
定义
template <class T, class = std::allocator<T>>
vector;
C++ STL vector 容器提供了动态数组的核心功能,包括头文件引入、类模板定义及成员类型。内容涵盖构造与析构、赋值操作、元素访问(含边界检查)、迭代器使用、容量管理(如 reserve、shrink_to_fit)以及修改器(push_back、insert、erase 等)。通过具体代码示例展示各接口用法与注意事项,适用于 C++ 开发者快速查阅 vector 标准库 API。
#include <vector>
template <class T, class = std::allocator<T>>
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。元素的析构函数被调用,并释放所使用的存储空间。请注意,如果元素是指针,则指向的对象不会被销毁。
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}
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}
allocator_type get_allocator() const; // 返回与容器关联的分配器
// 示例:std::vector<int> vec; std::allocator<int> alloc = vec.get_allocator();
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}
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
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
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
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
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();
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();
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();
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();
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_type size() const; // 返回容器中的元素数量,即 std::distance(begin(), end())。
// 示例:std::vector<int> vec = {1, 2, 3}; std::cout << vec.size(); // 输出 : 3
bool empty() const; // 检查容器是否没有元素,即 begin() == end() 是否为真。
// 示例:std::vector<int> vec; std::cout << vec.empty(); // 输出 : 1
size_type capacity() const; // 返回容器当前已分配空间所能容纳的元素数量。
// 示例:std::vector<int> vec = {1, 2, 3}; std::cout << vec.capacity(); // 输出 : 3(或更大)
void reserve(size_type new_cap); // 将 vector 的容量增加到大于或等于 new_cap 的值。reserve() 不会改变 vector 的大小。
// 示例:std::vector<int> vec; vec.reserve(5); std::cout << vec.capacity(); // 输出 : 5
void shrink_to_fit(); // 请求移除未使用的容量。这是一个非强制性的请求,要求将 capacity() 减小到 size()。
// 示例:std::vector<int> vec = {1, 2, 3}; vec.reserve(10); vec.shrink_to_fit();
size_type max_size() const; // 返回容器能够容纳的最大元素数量,受系统或库实现限制。
// 示例:std::vector<int> vec; std::cout << vec.max_size();
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]));
void pop_back(); // 移除容器的最后一个元素。在空容器上调用 pop_back 会导致未定义行为。
// 示例:std::vector<int> vec = {1, 2, 3}; vec.pop_back(); // vec = {1, 2}
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}
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});
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}
void clear(); // 从容器中擦除所有元素。在此调用之后,size() 返回零。使任何指向所含元素的引用、指针和迭代器失效。
// 示例:std::vector<int> vec = {1, 2, 3}; vec.clear(); // vec = {}
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}
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);
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);
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);
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);

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online
通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online