|
|
The vector class supports a dynamic array. Its template specification is shown here:
template <class T, class Allocator = allocator<T> > class vector
Here, T is the type of data being stored and Allocator specifies the allocator. It has the following constructors:
explicit vector(const Allocator &a = Allocator( ));
explicit vector(size_type num, const T &val = T ( ),
const Allocator &a = Allocator( ));
vector(const vector<T, Allocator> &ob);
template <class InIter> vector(InIter start, InIter end,
const Allocator &a = Allocator( ));
The first form constructs an empty vector. The second form constructs a vector that has num elements with the value val. The third form constructs a vector that contains the same elements as ob. The fourth form constructs a vector that contains the elements in the range specified by start and end.
The following comparison operators are defined for vector:
==, <, <=, !=, >, >=
vector contains the following member functions:
Member | Description |
---|---|
template <class InIter> | Assigns the vector the sequence defined by start and end. |
void assign(size_type num, const T &val); | Assigns the vector num elements of value val. |
reference at(size_type i); | Returns a reference to an element specified by i. |
reference back( ); | Returns a reference to the last element in the vector. |
iterator begin( ); | Returns an iterator to the first element in the vector. |
size_type capacity( ) const; | Returns the current capacity of the vector. This is the number of elements it can hold before it will need to allocate more memory. |
void clear( ); | Removes all elements from the vector. |
bool empty( ) const; | Returns true if the invoking vector is empty and false otherwise. |
iterator end( ); | Returns an iterator to the end of the vector. |
iterator erase(iterator i); | Removes the element pointed to by i. Returns an iterator to the element after the one removed. |
iterator erase(iterator start, iterator end); | Removes the elements in the range start to end. Returns an iterator to the element after the last element removed. |
reference front( ); | Returns a reference to the first element in the vector. |
allocator_type get_allocator( ) const; | Returns vector’s allocator. |
iterator insert(iterator i, const T &val); | Inserts val immediately before the element specified by i. An iterator to the element is returned. |
void insert(iterator i, size_type num, | Inserts num copies of val immediately before the element specified by i. |
template <class InIter> | Inserts the sequence defined by start and end immediately before the element specified by i. |
size_type max_size( ) const; | Returns the maximum number of elements that the vector can hold. |
reference operator[ ](size_type i) const; | Returns a reference to the element specified by i. |
void pop_back( ); | Removes the last element in the vector. |
void push_back(const T &val); | Adds an element with the value specified by val to the end of the vector. |
reverse_iterator rbegin( ); | Returns a reverse iterator to the end of the vector. |
reverse_iterator rend( ); | Returns a reverse iterator to the start of the vector. |
void reserve(size_type num); | Sets the capacity of the vector so that it is equal to at least num. |
void resize(size_type num, T val = T ( )); | Changes the size of the vector to that specified by num. If the vector must be lengthened, then elements with the value specified by val are added to the end. |
size_type size( ) const; | Returns the number of elements currently in the vector. |
void swap(vector<T, Allocator> &ob); | Exchanges the elements stored in the invoking vector with those in ob. |
The STL also contains a specialization of vector for Boolean values. It includes all of the functionality of vector and adds these two members:
void flip( ); | Reverses all bits in the vector. |
static void swap(reference i, reference j); | Exchanges the bits specified by i and j. |
|
|