标准库头文件 <vector>

来自cppreference.com
< cpp‎ | header
 
 
 

此头文件是容器库的一部分。

包含

<initializer_list>(C++11)

动态的连续数组
(类模板)
节省空间的动态 bitset
(类模板)
std::vector<bool> 的散列支持
(类模板特化)

函数

按照字典顺序比较 vector 中的值
(函数模板)
特化 std::swap 算法
(函数模板)

概要

#include <initializer_list>
namespace std {
    template <class T, class Allocator = allocator<T> > class vector;
 
    template <class T, class Allocator>
        bool operator==(const vector<T,Allocator>& x,const vector<T,Allocator>& y);
    template <class T, class Allocator>
        bool operator< (const vector<T,Allocator>& x,const vector<T,Allocator>& y);
    template <class T, class Allocator>
        bool operator!=(const vector<T,Allocator>& x,const vector<T,Allocator>& y);
    template <class T, class Allocator>
        bool operator> (const vector<T,Allocator>& x,const vector<T,Allocator>& y);
    template <class T, class Allocator>
        bool operator>=(const vector<T,Allocator>& x,const vector<T,Allocator>& y);
    template <class T, class Allocator>
        bool operator<=(const vector<T,Allocator>& x,const vector<T,Allocator>& y);
 
    template<class T, class Allocator>
        void swap(vector<T, Allocator>& x, vector<T, Allocator>& y)
            noexcept(noexcept(x.swap(y)));
 
    template <class T, class Allocator, class U>
        void erase(vector<T, Allocator>& c, const U& value);
    template <class T, class Allocator, class Predicate>
        void erase_if(vector<T, Allocator>& c, Predicate pred);
 
    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

template <class T, class Allocator = allocator<T> >
class vector {
public:
    // 类型:
    typedef value_type&                                         reference;
    typedef const value_type&                                   const_reference;
    typedef /* 由实现定义 */                                     iterator;
    typedef /* 由实现定义 */                                     const_iterator; 
    typedef /* 由实现定义 */                                     size_type;
    typedef /* 由实现定义 */                                     difference_type;
    typedef T                                                   value_type;
    typedef Allocator                                           allocator_type;
    typedef typename allocator_traits<Allocator>::pointer       pointer;
    typedef typename allocator_traits<Allocator>::const_pointer const_pointer;
    typedef std::reverse_iterator<iterator>                     reverse_iterator;
    typedef std::reverse_iterator<const_iterator>               const_reverse_iterator;
 
    // 构造/复制/销毁:
    explicit vector(const Allocator& = Allocator());
    explicit vector(size_type n);
    vector(size_type n, const T& value,const Allocator& = Allocator());
    template <class InputIt>
        vector(InputIt first, InputIt last,const Allocator& = Allocator());
    vector(const vector<T,Allocator>& x);
    vector(vector&&);
    vector(const vector&, const Allocator&);
    vector(vector&&, const Allocator&);
    vector(initializer_list<T>, const Allocator& = Allocator());
 
    ~vector();
    vector<T,Allocator>& operator=(const vector<T,Allocator>& x);
    vector<T,Allocator>& operator=(vector<T,Allocator>&& x);
    vector& operator=(initializer_list<T>);
    template <class InputIt>
        void assign(InputIt first, InputIt last);
    void assign(size_type n, const T& t);
    void assign(initializer_list<T>);
    allocator_type get_allocator() const noexcept;
 
    // 迭代器:
    iterator                begin() noexcept;
    const_iterator          begin() const noexcept;
    iterator                end() noexcept;
    const_iterator          end() const noexcept;
 
    reverse_iterator        rbegin() noexcept;
    const_reverse_iterator  rbegin() const noexcept;
    reverse_iterator        rend() noexcept;
    const_reverse_iterator  rend() const noexcept;
 
    const_iterator          cbegin() const noexcept;
    const_iterator          cend() const noexcept;
    const_reverse_iterator  crbegin() const noexcept;
    const_reverse_iterator  crend() const noexcept;
 
    // 容量:
    size_type size() const noexcept;
    size_type max_size() const noexcept;
    void      resize(size_type sz);
    void      resize(size_type sz, const T& c);
    size_type capacity() const noexcept;
    [[nodiscard]] bool empty() const noexcept;
    void      reserve(size_type n);
    void      shrink_to_fit();
 
    // 元素访问:
    reference       operator[](size_type n);
    const_reference operator[](size_type n) const;
    reference       at(size_type n);
    const_reference at(size_type n) const;
    reference       front();
    const_reference front() const;
    reference       back();
    const_reference back() const;
 
    // 数据访问:
    T*       data() noexcept;
    const T* data() const noexcept;
 
    // 修改器:
    template <class... Args> void emplace_back(Args&&... args);
    void push_back(const T& x);
    void push_back(T&& x);
    void pop_back();
 
    template <class... Args> iterator emplace(const_iterator position, Args&&... args);
    iterator insert(const_iterator position, const T& x);
    iterator insert(const_iterator position, T&& x);
    iterator insert(const_iterator position, size_type n, const T& x);
    template <class InputIt>
        iterator insert (const_iterator position, InputIt first, 
                         InputIt last);
    iterator insert(const_iterator position, initializer_list<T>);
 
    iterator erase(const_iterator position);
    iterator erase(const_iterator first, const_iterator last);
    void     swap(vector<T,Allocator>&);
    void     clear() noexcept;
};
 
template<class InputIt,
         class Allocator = allocator</*iter-value-type*/<InputIt>>>
    vector(InputIt, InputIt, Allocator = Allocator())
        -> vector</*iter-value-type*/<InputIt>, Allocator>;

特化 std::vector<bool>

template < class Allocator >
class vector<bool, Allocator> {
public:
    // 类型:
    typedef bool                                                const_reference;
    typedef /* 由实现定义 */                                     iterator;
    typedef /* 由实现定义 */                                     const_iterator; 
    typedef /* 由实现定义 */                                     size_type;
    typedef /* 由实现定义 */                                     difference_type;
    typedef bool                                                value_type;
    typedef Allocator                                           allocator_type;
    typedef /* 由实现定义 */                                     pointer;
    typedef /* 由实现定义 */                                     const_pointer;
    typedef std::reverse_iterator<iterator>                     reverse_iterator;
    typedef std::reverse_iterator<const_iterator>               const_reverse_iterator;
 
    // 位引用:
    class reference {
        friend class vector;
        reference() noexcept;
    public:
        ~reference();
        operator bool() const noexcept;
        reference& operator=(const bool x) noexcept;
        reference& operator=(const reference& x) noexcept;
        void flip() noexcept; // 翻转位
    };
 
    // 构造/复制/销毁:
    explicit vector(const Allocator& = Allocator());
    explicit vector(size_type n);
    vector(size_type n, const bool& value,const Allocator& = Allocator());
    template <class InputIt>
        vector(InputIt first, InputIt last,const Allocator& = Allocator());
    vector(const vector<bool,Allocator>& x);
    vector(vector&&);
    vector(const vector&, const Allocator&);
    vector(vector&&, const Allocator&);
    vector(initializer_list<bool>, const Allocator& = Allocator());
 
    ~vector();
    vector<bool,Allocator>& operator=(const vector<bool,Allocator>& x);
    vector<bool,Allocator>& operator=(vector<bool,Allocator>&& x);
    vector& operator=(initializer_list<bool>);
    template <class InputIt>
        void assign(InputIt first, InputIt last);
    void assign(size_type n, const bool& t);
    void assign(initializer_list<bool>);
    allocator_type get_allocator() const noexcept;
 
    // 迭代器:
    iterator                begin() noexcept;
    const_iterator          begin() const noexcept;
    iterator                end() noexcept;
    const_iterator          end() const noexcept;
 
    reverse_iterator        rbegin() noexcept;
    const_reverse_iterator  rbegin() const noexcept;
    reverse_iterator        rend() noexcept;
    const_reverse_iterator  rend() const noexcept;
 
    const_iterator          cbegin() const noexcept;
    const_iterator          cend() const noexcept;
    const_reverse_iterator  crbegin() const noexcept;
    const_reverse_iterator  crend() const noexcept;
 
    // 容量:
    size_type size() const noexcept;
    size_type max_size() const noexcept;
    void      resize(size_type sz);
    void      resize(size_type sz, const bool& c);
    size_type capacity() const noexcept;
    [[nodiscard]] bool empty() const noexcept;
    void      reserve(size_type n);
    void      shrink_to_fit();
 
    // 元素访问:
    reference       operator[](size_type n);
    const_reference operator[](size_type n) const;
    reference       at(size_type n);
    const_reference at(size_type n) const;
    reference       front();
    const_reference front() const;
    reference       back();
    const_reference back() const;
 
    // 修改器:
    template <class... Args> void emplace_back(Args&&... args);
    void push_back(const bool& x);
    void push_back(bool&& x);
    void pop_back();
 
    template <class... Args> iterator emplace(const_iterator position, Args&&... args);
    iterator insert(const_iterator position, const bool& x);
    iterator insert(const_iterator position, bool&& x);
    iterator insert(const_iterator position, size_type n, const bool& x);
    template <class InputIt>
        iterator insert (const_iterator position, InputIt first, 
                         InputIt last);
    iterator insert(const_iterator position, initializer_list<bool>);
 
    iterator erase(const_iterator position);
    iterator erase(const_iterator first, const_iterator last);
    void     swap(vector<bool,Allocator>&);
    static void swap(reference x, reference y) noexcept;
    void     flip() noexcept;// 翻转所有位
    void     clear() noexcept;
};