标准库头文件 <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 /*implementation-defined*/                          iterator;
    typedef /*implementation-defined*/                          const_iterator; 
    typedef /*implementation-defined*/                          size_type;
    typedef /*implementation-defined*/                          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 /*implementation-defined*/                          iterator;
    typedef /*implementation-defined*/                          const_iterator; 
    typedef /*implementation-defined*/                          size_type;
    typedef /*implementation-defined*/                          difference_type;
    typedef bool                                                value_type;
    typedef Allocator                                           allocator_type;
    typedef /*implementation-defined*/                          pointer;
    typedef /*implementation-defined*/                          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;
};