标准库头文件 <vector>
此头文件是容器库的一部分。
包含 | |
(C++20) |
三路比较运算符支持 |
(C++11) |
std::initializer_list 类模板 |
类 | |
动态的连续数组 (类模板) | |
节省空间的动态 bitset (类模板特化) | |
(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) |
返回容器或数组的大小 (函数模板) |
(C++17) |
检查容器是否为空 (函数模板) |
(C++17) |
获得指向底层数组的指针 (函数模板) |
概要
#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> 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); // 类 vector<bool> template<class Allocator> class vector<bool, Allocator>; // 散列支持 template<class T> struct hash; template<class Allocator> struct hash<vector<bool, Allocator>>; namespace pmr { template<class T> using vector = std::vector<T, polymorphic_allocator<T>>; } }
类模板 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 InputIt> constexpr vector(InputIt first, InputIt last, const Allocator& = Allocator()); constexpr vector(const vector& x); constexpr vector(vector&&) noexcept; constexpr vector(const vector&, const Allocator&); constexpr vector(vector&&, const 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 InputIt> constexpr void assign(InputIt first, InputIt last); 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; // 容量 [[nodiscard]] constexpr bool empty() const noexcept; constexpr size_type size() const noexcept; constexpr size_type max_size() const noexcept; constexpr size_type capacity() const noexcept; constexpr void resize(size_type sz); constexpr void resize(size_type sz, const T& c); constexpr void reserve(size_type n); constexpr void shrink_to_fit(); // 元素访问 constexpr reference operator[](size_type n); constexpr const_reference operator[](size_type n) const; constexpr const_reference at(size_type n) const; constexpr reference at(size_type n); constexpr reference front(); constexpr const_reference front() const; constexpr reference back(); constexpr const_reference back() const; // 数据访问 constexpr T* data() noexcept; constexpr const T* data() const noexcept; // 修改器 template<class... Args> constexpr reference emplace_back(Args&&... args); constexpr void push_back(const T& x); constexpr void push_back(T&& x); constexpr void pop_back(); template<class... Args> constexpr iterator emplace(const_iterator position, Args&&... args); constexpr iterator insert(const_iterator position, const T& x); constexpr iterator insert(const_iterator position, T&& x); constexpr iterator insert(const_iterator position, size_type n, const T& x); template<class InputIt> constexpr iterator insert(const_iterator position, InputIt first, InputIt last); constexpr iterator insert(const_iterator position, initializer_list<T> il); constexpr iterator erase(const_iterator position); constexpr iterator erase(const_iterator first, const_iterator last); constexpr void swap(vector&) noexcept(allocator_traits<Allocator>::propagate_on_container_swap::value || allocator_traits<Allocator>::is_always_equal::value); constexpr void clear() noexcept; }; template<class InputIt, class Allocator = allocator</*iter-value-type*/<InputIt>>> vector(InputIt, InputIt, Allocator = Allocator()) -> vector</*iter-value-type*/<InputIt>, Allocator>; // 交换 template<class T, class Allocator> constexpr void swap(vector<T, Allocator>& x, vector<T, Allocator>& y) noexcept(noexcept(x.swap(y))); }
类模板 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 { friend class vector; constexpr reference() noexcept; public: constexpr reference(const reference&) = default; constexpr ~reference(); constexpr operator bool() const noexcept; constexpr reference& operator=(const bool x) noexcept; constexpr reference& operator=(const reference& x) noexcept; constexpr void flip() noexcept; // 翻转位 }; // 构造/复制/销毁 constexpr vector() : vector(Allocator()) { } constexpr explicit vector(const Allocator&); constexpr explicit vector(size_type n, const Allocator& = Allocator()); constexpr vector(size_type n, const bool& value, const Allocator& = Allocator()); template<class InputIt> constexpr vector(InputIt first, InputIt last, const Allocator& = Allocator()); constexpr vector(const vector& x); constexpr vector(vector&& x); constexpr vector(const vector&, const Allocator&); constexpr vector(vector&&, const Allocator&); constexpr vector(initializer_list<bool>, const Allocator& = Allocator())); constexpr ~vector(); constexpr vector& operator=(const vector& x); constexpr vector& operator=(vector&& x); constexpr vector& operator=(initializer_list<bool>); template<class InputIt> constexpr void assign(InputIt first, InputIt last); 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 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; // 容量 [[nodiscard]] constexpr bool empty() const noexcept; constexpr size_type size() const noexcept; constexpr size_type max_size() const noexcept; constexpr size_type capacity() const noexcept; constexpr void resize(size_type sz, bool c = false); constexpr void reserve(size_type n); constexpr void shrink_to_fit(); // 元素访问 constexpr reference operator[](size_type n); constexpr const_reference operator[](size_type n) const; constexpr const_reference at(size_type n) const; constexpr reference at(size_type n); constexpr reference front(); constexpr const_reference front() const; constexpr reference back(); constexpr const_reference back() const; // 修改器 template<class... Args> constexpr reference emplace_back(Args&&... args); constexpr void push_back(const bool& x); constexpr void pop_back(); template<class... Args> constexpr iterator emplace(const_iterator position, Args&&... args); constexpr iterator insert(const_iterator position, const bool& x); constexpr iterator insert(const_iterator position, size_type n, const bool& x); template<class InputIt> constexpr iterator insert(const_iterator position, InputIt first, InputIt last); constexpr iterator insert(const_iterator position, initializer_list<bool> il); constexpr iterator erase(const_iterator position); constexpr iterator erase(const_iterator first, const_iterator last); constexpr void swap(vector&); constexpr static void swap(reference x, reference y) noexcept; constexpr void flip() noexcept; // 翻转所有位 constexpr void clear() noexcept; }; }