Vector
Home Up

 

Same level pages:
Vector
Deque
List
Set
Multiset
Map
Multimap
String
Bitset
Stack
Queue
Priority Queue

Parent level pages:
Containers
Classes
Iterators
Algorithms

The template class vector<T> is a random access container and a back insertion sequence. Its elements must be assignable and default constructible.

The STL vector template is found in <vector>. A simplified declaration of the vector template would look something like:

template<class T>
class vector
{
public:
    vector();
    vector(size_type n, const T& v = T());
    vector(const vector& x);
    void reserve(size_type n);
    unsigned capacity() const;
    void resize(unsigned n, T x = T());
    unsigned size() const;
    unsigned max_size() const;
    bool empty() const;
    void assign(unsigned n, const T& x = T());
    void clear();
    void swap(vector x);
    T& front();
    const T& front() const;
    T& back();
    const T& back() const;
    // random access container
    T& operator[](unsigned pos);
    const T& operator[](unsigned pos);
    // a back insertion sequence
    void push_back(const T& x);
    void pop_back();

};

Since vector is a container class, it supports iterators. Optionally, an allocator other than ::new() can be used with the template. The full declaration looks something like:

template<class T, class A = allocator<T> >
class vector
{
public:
    typedef A allocator_type;
    typedef A::size_type size_type;
    typedef A::difference_type difference_type;
    typedef A::reference reference;
    typedef A::const_reference const_reference;
    typedef A::value_type value_type;
    typedef T0 iterator;
    typedef T1 const_iterator;
    typedef reverse_iterator<iterator, value_type,
        reference, A::pointer, difference_type>
            reverse_iterator;
    typedef reverse_iterator<const_iterator, value_type,
        const_reference, A::const_pointer, difference_type>
            const_reverse_iterator;
    explicit vector(const A& al = A());
    explicit vector(size_type n, const T& v = T(), const A& al = A());
    vector(const vector& x);
    vector(const_iterator first, const_iterator last,
        const A& al = A());
    void reserve(size_type n);
    size_type capacity() const;
    iterator begin();
    const_iterator begin() const;
    iterator end();
    iterator end() const;
    reverse_iterator rbegin();
    const_reverse_iterator rbegin() const;
    reverse_iterator rend();
    const_reverse_iterator rend() const;
    void resize(size_type n, T x = T());
    size_type size() const;
    size_type max_size() const;
    bool empty() const;
    A get_allocator() const;
    reference >at(size_type pos);
    const_reference at(size_type pos) const;
    reference operator[](size_type pos);
    const_reference operator[](size_type pos);
    reference front();
    const_reference front() const;
    reference back();
    const_reference back() const;
    void push_back(const T& x);
    void pop_back();
    void assign(const_iterator first, const_iterator last);
    void assign(size_type n, const T& x = T());
    iterator insert(iterator it, const T& x = T());
    void insert(iterator it, size_type n, const T& x);
    void insert(iterator it,
        const_iterator first, const_iterator last);
    iterator erase(iterator it);
    iterator erase(iterator first, iterator last);
    void clear();
    void swap(vector x);
protected:
    A allocator;
};
 
ŠPaul Buis & Ball State University Author: Paul Buis (peb@bsu.edu) Last Modified:10/26/00 03:31 PM

NOTICE: The information presented on this page represents the personal views, ideas, and opinions of the author. This is not an official Ball State University web page. Links contained at this web site to other organizations, are presented as a service and neither constitute nor imply university endorsement or warranty.