Standard library header <vector>
From cppreference.net
此头文件是 容器 库的组成部分。
包含文件 |
||
|
(C++20)
|
三路比较运算符 支持 | |
|
(C++11)
|
std::initializer_list 类模板 | |
类 |
||
|
可调整大小的连续数组
(类模板) |
||
|
节省空间的动态位集
(类模板特化) |
||
|
(C++11)
|
std::
vector
<
bool
>
的哈希支持
(类模板特化) |
|
前向声明 |
||
|
定义于头文件
<functional>
|
||
|
(C++11)
|
哈希函数对象
(类模板) |
|
函数 |
||
|
(C++20 中移除)
(C++20 中移除)
(C++20 中移除)
(C++20 中移除)
(C++20 中移除)
(C++20)
|
按字典序比较两个
vector
的值
(函数模板) |
|
|
特化
std::swap
算法
(函数模板) |
||
|
擦除所有满足特定条件的元素
(函数模板) |
||
范围访问 |
||
|
(C++11)
(C++14)
|
返回指向容器或数组起始的迭代器
(函数模板) |
|
|
(C++11)
(C++14)
|
返回指向容器或数组末尾的迭代器
(函数模板) |
|
|
(C++14)
|
返回指向容器或数组起始的反向迭代器
(函数模板) |
|
|
(C++14)
|
返回容器或数组的逆向末尾迭代器
(函数模板) |
|
|
(C++17)
(C++20)
|
返回容器或数组的大小
(函数模板) |
|
概要
#include <compare> #include <initializer_list> namespace std { // 类模板 vector template<class T, class Allocator = allocator<T>> class vector; template<class T, class Allocator> constexpr bool operator==(const vector<T, Allocator>& x, const vector<T, Allocator>& y); template<class T, class Allocator> constexpr /*synth-three-way-result*/<T> operator<=>(const vector<T, Allocator>& x, const vector<T, Allocator>& y); template<class T, class Allocator> constexpr void swap(vector<T, Allocator>& x, vector<T, Allocator>& y) noexcept(noexcept(x.swap(y))); // 擦除 template<class T, class Allocator, class U = T> constexpr typename vector<T, Allocator>::size_type erase(vector<T, Allocator>& c, const U& value); template<class T, class Allocator, class Predicate> constexpr typename vector<T, Allocator>::size_type erase_if(vector<T, Allocator>& c, Predicate pred); namespace pmr { template<class T> using vector = std::vector<T, polymorphic_allocator<T>>; } // bool 类型的 vector 特化 // 部分类模板特化 vector<bool, Allocator> template<class Allocator> class vector<bool, Allocator>; template<class T> constexpr bool /*is-vector-bool-reference*/ = /* 见描述 */; // 仅用于说明 // 哈希支持 template<class T> struct hash; template<class Allocator> struct hash<vector<bool, Allocator>>; // vector<bool> 的格式化器特化 template<class T, class CharT> requires /*is-vector-bool-reference*/<T> struct formatter<T, CharT>; }
类模板 std::vector
namespace std { template<class T, class Allocator = allocator<T>> class vector { public: // 类型定义 using value_type = T; using allocator_type = Allocator; using pointer = typename allocator_traits<Allocator>::pointer; using const_pointer = typename allocator_traits<Allocator>::const_pointer; using reference = value_type&; using const_reference = const value_type&; using size_type = /* 实现定义 */; using difference_type = /* 实现定义 */; using iterator = /* 实现定义 */; using const_iterator = /* 实现定义 */; using reverse_iterator = std::reverse_iterator<iterator>; using const_reverse_iterator = std::reverse_iterator<const_iterator>; // 构造/复制/销毁 constexpr vector() noexcept(noexcept(Allocator())) : vector(Allocator()) { } constexpr explicit vector(const Allocator&) noexcept; constexpr explicit vector(size_type n, const Allocator& = Allocator()); constexpr vector(size_type n, const T& value, const Allocator& = Allocator()); template<class InputIter> constexpr vector(InputIter first, InputIter last, const Allocator& = Allocator()); template<container-compatible-range<T> R> constexpr vector(from_range_t, R&& rg, const Allocator& = Allocator()); constexpr vector(const vector& x); constexpr vector(vector&&) noexcept; constexpr vector(const vector&, const type_identity_t<Allocator>&); constexpr vector(vector&&, const type_identity_t<Allocator>&); constexpr vector(initializer_list<T>, const Allocator& = Allocator()); constexpr ~vector(); constexpr vector& operator=(const vector& x); constexpr vector& operator=(vector&& x) noexcept( allocator_traits<Allocator>::propagate_on_container_move_assignment::value || allocator_traits<Allocator>::is_always_equal::value); constexpr vector& operator=(initializer_list<T>); template<class InputIter> constexpr void assign(InputIter first, InputIter last); template<container-compatible-range<T> R> constexpr void assign_range(R&& rg); constexpr void assign(size_type n, const T& u); constexpr void assign(initializer_list<T>); constexpr allocator_type get_allocator() const noexcept; // 迭代器 constexpr iterator begin() noexcept; constexpr const_iterator begin() const noexcept; constexpr iterator end() noexcept; constexpr const_iterator end() const noexcept; constexpr reverse_iterator rbegin() noexcept; constexpr const_reverse_iterator rbegin() const noexcept; constexpr reverse_iterator rend() noexcept; constexpr const_reverse_iterator rend() const noexcept; constexpr const_iterator cbegin() const noexcept; constexpr const_iterator cend() const noexcept; constexpr const_reverse_iterator crbegin() const noexcept; constexpr const_reverse_iterator crend() const noexcept; // 容量 constexpr bool empty<span class="br
类模板 std::vector 针对 bool 的特化
namespace std { template<class Allocator> class vector<bool, Allocator> { public: // 类型定义 using value_type = bool; using allocator_type = Allocator; using pointer = /* 实现定义 */; using const_pointer = /* 实现定义 */; using const_reference = bool; using size_type = /* 实现定义 */; using difference_type = /* 实现定义 */; using iterator = /* 实现定义 */; using const_iterator = /* 实现定义 */; using reverse_iterator = std::reverse_iterator<iterator>; using const_reverse_iterator = std::reverse_iterator<const_iterator>; // 位引用 class reference { public: constexpr reference(const reference&) = default; constexpr ~reference(); constexpr operator bool() const noexcept; constexpr reference& operator=(bool x) noexcept; constexpr reference& operator=(const reference& x) noexcept; constexpr const reference& operator=(bool x) const noexcept; constexpr void flip() noexcept; // 翻转位 }; // 构造/复制/销毁 constexpr vector() noexcept(noexcept(Allocator())) : vector(Allocator()) { } constexpr explicit vector(const Allocator&) noexcept; constexpr explicit vector(size_type n, const Allocator& = Allocator()); constexpr vector(size_type n, const bool& value, const Allocator& = Allocator()); template<class InputIter> constexpr vector(InputIter first, InputIter last, const Allocator& = Allocator()); template<container-compatible-range<bool> R> constexpr vector(from_range_t, R&& rg, const Allocator& = Allocator()); constexpr vector(const vector& x); constexpr vector(vector&& x) noexcept; constexpr vector(const vector&, const type_identity_t<Allocator>&); constexpr vector(vector&&, const type_identity_t<Allocator>&); constexpr vector(initializer_list<bool>, const Allocator& = Allocator()); constexpr ~vector(); constexpr vector& operator=(const vector& x); constexpr vector& operator=(vector&& x) noexcept( allocator_traits<Allocator>::propagate_on_container_move_assignment::value || allocator_traits<Allocator>::is_always_equal::value); constexpr vector& operator=(initializer_list<bool>); template<class InputIter> constexpr void assign(InputIter first, InputIter last); template<container-compatible-range<bool> R> constexpr void assign_range(R&& rg); constexpr void assign(size_type n, const bool& t); constexpr void assign(initializer_list<bool>); constexpr allocator_type get_allocator() const noexcept; // 迭代器 constexpr iterator begin() noexcept; constexpr const_iterator begin() const noexcept; constexpr iterator end() noexcept; constexpr const_iterator end() const <span class="kw
类模板 std::formatter 对 std::vector < bool > :: reference 的特化
namespace std { template<class T, class CharT> requires /*is-vector-bool-reference*/<T> struct formatter<T, CharT> { private: formatter<bool, CharT> /*underlying_*/; // 仅用于说明 public: template<class ParseContext> constexpr typename ParseContext::iterator parse(ParseContext& ctx); template<class FormatContext> typename FormatContext::iterator format(const T& ref, FormatContext& ctx) const; }; }
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
| DR | 适用范围 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 4140 | C++98 | 概要说明中包含 std:: vector < bool , Alloc > :: reference 默认构造函数的声明 | 移除了该声明 |