C++-Referenz/ Standardbibliothek/ Container-Klassen/ vector

Alte Seite

Die Arbeit am Buch »C++-Referenz« wurde vom Hauptautor eingestellt. Ein Lehrbuch zum Thema C++ ist unter »C++-Programmierung« zu finden. Eine sehr umfangreiche und gute Referenz gibt es unter cppreference.com.

Diese Seite beschreibt C++98, einen stark veralteten Standard. Aktuelle Referenz.

// Header: vector
template <typename T, typename Allocator = allocator<T> >
class vector {
public:
    typedef typename Allocator::reference         reference;
    typedef typename Allocator::const_reference   const_reference;
    typedef implementation defined                iterator;
    typedef implementation defined                const_iterator;
    typedef typename Allocator::size_type         size_type;
    typedef typename Allocator::difference_type   difference_type;
    typedef T                                     value_type;
    typedef Allocator                             allocator_type;
    typedef typename Allocator::pointer           pointer;
    typedef typename Allocator::const_pointer     const_pointer
    typedef std::reverse_iterator<iterator>       reverse_iterator;
    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

// Konstruktoren, Destruktor, Kopieren
    explicit vector(Allocator const& = Allocator());

    explicit vector(size_type n, T const& value = T(), Allocator const& = Allocator());

    template <typename InputIterator>
    vector(InputIterator first, InputIterator last, Allocator const& = Allocator());

    vector(vector<T,Allocator> const& x);

    ~vector();

    vector<T,Allocator>& operator=(vector<T,Allocator> const& x);

    template <typename InputIterator>    void assign(InputIterator first, InputIterator last);
    template <typename Size, typename U> void assign(Size n, U const& u = U());

    allocator_type get_allocator()const;

// Iteratoren
    iterator       begin();
    const_iterator begin()const;
    iterator       end();
    const_iterator end()const;

    reverse_iterator       rbegin();
    const_reverse_iterator rbegin()const;
    reverse_iterator       rend();
    const_reverse_iterator rend()const;

// Kapazität
    bool      empty()const;
    size_type size()const;
    size_type max_size()const;
    size_type capacity()const;

    void resize(size_type sz, T c = T());
    void reserve(size_type n);

// Elementzugriff
    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;

// Modifizierer
    void push_back(T const& x);
    void pop_back();

    iterator insert(iterator position, T const& x = T());
    void     insert(iterator position, size_type n, T const& x);
    template <typename InputIterator>
    void     insert(iterator position, InputIterator first, InputIterator last);

    iterator erase(iterator position);
    iterator erase(iterator first, iterator last);

    void swap(vector<T,Allocator>&);

    void clear();
};

// Vergleiche
template <typename T, typename Allocator>
bool operator==(vector<T,Allocator> const& x, vector<T,Allocator> const& y);

template <typename T, typename Allocator>
bool operator< (vector<T,Allocator> const& x, vector<T,Allocator> const& y);

template <typename T, typename Allocator>
bool operator!=(vector<T,Allocator> const& x, vector<T,Allocator> const& y);

template <typename T, typename Allocator>
bool operator> (vector<T,Allocator> const& x, vector<T,Allocator> const& y);

template <typename T, typename Allocator>
bool operator>=(vector<T,Allocator> const& x, vector<T,Allocator> const& y);

template <typename T, typename Allocator>
bool operator<=(vector<T,Allocator> const& x, vector<T,Allocator> const& y);

// Spezialisierte Algorithmen
template <typename T, typename Allocator>
void swap(vector<T,Allocator>& x, vector<T,Allocator>& y);

Spezialisierung für bool

Bearbeiten
// Header: vector
template <typename Allocator = allocator<bool> >
class vector<bool, Allocator>{
public:
    typedef bool                                  const_reference;
    typedef implementation defined                iterator;
    typedef implementation defined                const_iterator;
    typedef typename Allocator::size_type         size_type;
    typedef typename Allocator::difference_type   difference_type;
    typedef bool                                  value_type;
    typedef Allocator                             allocator_type;
    typedef typename Allocator::pointer           pointer;
    typedef typename Allocator::const_pointer     const_pointer
    typedef std::reverse_iterator<iterator>       reverse_iterator;
    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

// Bit-Referenz
    class reference{
    public:
        ~reference();

        operator bool()const;

        reference& operator=(bool const x);
        reference& operator=(reference const& x);

        void flip();

    private:
        reference();

    friend class vector;
    };

// Konstruktoren, Destruktor, Kopieren
    explicit vector(Allocator const& = Allocator());

    explicit vector(size_type n, bool const& value = bool(), Allocator const& = Allocator());

    template <typename InputIterator>
    vector(InputIterator first, InputIterator last, Allocator const& = Allocator());

    vector(vector<bool,Allocator> const& x);

    ~vector();

    vector<bool,Allocator>& operator=(vector<bool,Allocator> const& x);

    template <typename InputIterator> void    assign(InputIterator first, InputIterator last);
    template <typename Size, typename U> void assign(Size n, U const& u = U());

    allocator_type get_allocator()const;

// Iteratoren
    iterator       begin();
    const_iterator begin()const;
    iterator       end();
    const_iterator end()const;

    reverse_iterator       rbegin();
    const_reverse_iterator rbegin()const;
    reverse_iterator       rend();
    const_reverse_iterator rend()const;

// Kapazität
    size_type size()const;
    size_type max_size()const;
    size_type capacity()const;
    bool      empty()const;

    void resize(size_type sz, bool c = false);

    void reserve(size_type n);

// Elementzugriff
    reference       operator[](size_type n);
    const_reference operator[](size_type n)const;
    const_reference at(size_type n)const;
    reference       at(size_type n);

    reference       front();
    const_reference front()const;
    reference       back();
    const_reference back()const;

// Modifizierer
    void push_back(bool const& x);
    void pop_back();

    iterator insert(iterator position, bool const& x = bool());
    void     insert(iterator position, size_type n, bool const& x);
    template <typename InputIterator>
    void     insert(iterator position, InputIterator first, InputIterator last)

    iterator erase(iterator position);
    iterator erase(iterator first, iterator last);

    void        swap(vector<bool,Allocator>&);
    static void swap(reference x, reference y);

    void flip();

    void clear();
};

// Vergleiche
template <typename Allocator>
bool operator==(vector<bool,Allocator> const& x, vector<bool,Allocator> const& y);

template <typename Allocator>
bool operator< (vector<bool,Allocator> const& x, vector<bool,Allocator> const& y);

template <typename Allocator>
bool operator!=(vector<bool,Allocator> const& x, vector<bool,Allocator> const& y);

template <typename Allocator>
bool operator> (vector<bool,Allocator> const& x, vector<bool,Allocator> const& y);

template <typename Allocator>
bool operator>=(vector<bool,Allocator> const& x, vector<bool,Allocator> const& y);

template <typename Allocator>
bool operator<=(vector<bool,Allocator> const& x, vector<bool,Allocator> const& y);

// Spezialisierte Algorithmen
template <typename Allocator>
void swap(vector<bool,Allocator>& x, vector<bool,Allocator>& y);