# vector Class Template Reference
[Containers, Sequences]

Inheritance diagram for vector:

List of all members.

## Detailed Description

### template<typename _Tp, typename _Alloc = allocator<_Tp>>
class std::vector< _Tp, _Alloc >

Meets the requirements of a container, a reversible container, and a sequence, including the optional sequence requirements with the exception of `push_front` and `pop_front`.

In some terminology a vector can be described as a dynamic C-style array, it offers fast and efficient access to individual elements in any order and saves the user from worrying about memory and size allocation. Subscripting ( ``[] ) access is also provided as with C-style arrays.

Definition at line 148 of file stl_vector.h.

## Public Types

- typedef _Base::allocator_type allocator_type
- typedef __gnu_cxx::__normal_iterator<
  const_pointer, vector_type > const_iterator
- typedef _Alloc::const_pointer const_pointer
- typedef _Alloc::const_reference const_reference
- typedef std::reverse_iterator<
  const_iterator > const_reverse_iterator
- typedef ptrdiff_t difference_type
- typedef __gnu_cxx::__normal_iterator<
  pointer, vector_type > iterator
- typedef _Alloc::pointer pointer
- typedef _Alloc::reference reference
- typedef std::reverse_iterator<
  iterator > reverse_iterator
- typedef size_t size_type
- typedef _Tp value_type

## Public Member Functions

- template<typename _InputIterator> void assign (_InputIterator __first, _InputIterator __last)
- void assign (size_type __n, const value_type &__val)
- const_reference at (size_type __n) const
- reference at (size_type __n)
- const_reference back () const
- reference back ()
- const_iterator begin () const
- iterator begin ()
- size_type capacity () const
- void clear ()
- bool empty () const
- const_iterator end () const
- iterator end ()
- iterator erase (iterator __first, iterator __last)
- iterator erase (iterator __position)
- const_reference front () const
- reference front ()
- template<typename _InputIterator> void insert (iterator __position, _InputIterator __first, _InputIterator __last)
- void insert (iterator __position, size_type __n, const value_type &__x)
- iterator insert (iterator __position, const value_type &__x)
- size_type max_size () const
- vector & operator= (const vector &__x)
- const_reference operator[] (size_type __n) const
- reference operator[] (size_type __n)
- void pop_back ()
- void push_back (const value_type &__x)
- const_reverse_iterator rbegin () const
- reverse_iterator rbegin ()
- const_reverse_iterator rend () const
- reverse_iterator rend ()
- void reserve (size_type __n)
- void resize (size_type __new_size)
- void resize (size_type __new_size, const value_type &__x)
- size_type size () const
- void swap (vector &__x)
- template<typename _InputIterator> vector (_InputIterator __first, _InputIterator __last, const allocator_type &__a=allocator_type())
- vector (const vector &__x)
- vector (size_type __n)
- vector (size_type __n, const value_type &__value, const allocator_type &__a=allocator_type())
- vector (const allocator_type &__a=allocator_type())
- ~vector ()

## Protected Member Functions

- template<typename _ForwardIterator> pointer _M_allocate_and_copy (size_type __n, _ForwardIterator __first, _ForwardIterator __last)
- template<typename _ForwardIterator> void _M_assign_aux (_ForwardIterator __first, _ForwardIterator __last, forward_iterator_tag)
- template<typename _InputIterator> void _M_assign_aux (_InputIterator __first, _InputIterator __last, input_iterator_tag)
- template<typename _InputIterator> void _M_assign_dispatch (_InputIterator __first, _InputIterator __last, __false_type)
- template<typename _Integer> void _M_assign_dispatch (_Integer __n, _Integer __val, __true_type)
- void _M_fill_assign (size_type __n, const value_type &__val)
- void _M_fill_insert (iterator __pos, size_type __n, const value_type &__x)
- template<typename _InputIterator> void _M_initialize_dispatch (_InputIterator __first, _InputIterator __last, __false_type)
- template<typename _Integer> void _M_initialize_dispatch (_Integer __n, _Integer __value, __true_type)
- void _M_insert_aux (iterator __position, const value_type &__x)
- template<typename _InputIterator> void _M_insert_dispatch (iterator __pos, _InputIterator __first, _InputIterator __last, __false_type)
- template<typename _Integer> void _M_insert_dispatch (iterator __pos, _Integer __n, _Integer __val, __true_type)
- void _M_range_check (size_type __n) const
- template<typename _ForwardIterator> void _M_range_initialize (_ForwardIterator __first, _ForwardIterator __last, forward_iterator_tag)
- template<typename _InputIterator> void _M_range_initialize (_InputIterator __first, _InputIterator __last, input_iterator_tag)
- template<typename _ForwardIterator> void _M_range_insert (iterator __pos, _ForwardIterator __first, _ForwardIterator __last, forward_iterator_tag)
- template<typename _InputIterator> void _M_range_insert (iterator __pos, _InputIterator __first, _InputIterator __last, input_iterator_tag)

## Constructor & Destructor Documentation

vector | ( | const allocator_type & | __a = `allocator_type()` | ) | `[inline, explicit]`

| Default constructor creates no elements. Definition at line 188 of file stl_vector.h.

vector | ( | size_type | __n,
|  | const value_type & | __value,
|  | const allocator_type & | __a = `allocator_type()`
| ) | `[inline]`

Create a vector with copies of an exemplar element.

Parameters:

  | n | The number of elements to initially create.
  | value | An element to copy.

This constructor fills the vector with n copies of value.

Definition at line 199 of file stl_vector.h.

vector | ( | size_type | __n | ) | `[inline, explicit]`

Create a vector with default elements.

Parameters:

  | n | The number of elements to initially create.

This constructor fills the vector with n copies of a default-constructed element.

Definition at line 216 of file stl_vector.h.

vector | ( | const vector< _Tp, _Alloc > & | __x | ) | `[inline]`

Vector copy constructor.

Parameters:

  | x | A vector of identical element and allocator types.

The newly-created vector uses a copy of the allocation object used by x. All the elements of x are copied, but any extra memory in x (for fast expansion) will not be copied.

Definition at line 233 of file stl_vector.h.

vector | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | const allocator_type & | __a = `allocator_type()`
| ) | `[inline]`

Builds a vector from a range.

Parameters:

  | first | An input iterator.
  | last | An input iterator.

Create a vector consisting of copies of the elements from [first,last).

If the iterators are forward, bidirectional, or random-access, then this will call the elements' copy constructor N times (where N is distance(first,last)) and do no memory reallocation. But if only input iterators are used, then this will do at most 2N calls to the copy constructor, and logN memory reallocations.

Definition at line 257 of file stl_vector.h.

~vector | ( |  | ) | `[inline]`

| The dtor only erases the elements, and note that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibilty. Definition at line 272 of file stl_vector.h.

## Member Function Documentation

void assign | ( | _InputIterator | __first,
|  | _InputIterator | __last
| ) | `[inline]`

Assigns a range to a vector.

Parameters:

  | first | An input iterator.
  | last | An input iterator.

This function fills a vector with copies of the elements in the range [first,last).

Note that the assignment completely changes the vector and that the resulting vector's size is the same as the number of elements assigned. Old data may be lost.

Definition at line 316 of file stl_vector.h.

void assign | ( | size_type | __n,
|  | const value_type & | __val
| ) | `[inline]`

Assigns a given value to a vector.

Parameters:

  | n | Number of elements to be assigned.
  | val | Value to be assigned.

This function fills a vector with n copies of the given value. Note that the assignment completely changes the vector and that the resulting vector's size is the same as the number of elements assigned. Old data may be lost.

Definition at line 299 of file stl_vector.h.

const_reference at | ( | size_type | __n | ) | const`[inline]`

Provides access to the data contained in the vector.

Parameters:

  | n | The index of the element for which data should be accessed.

Returns:
  Read-only (constant) reference to data.

Exceptions:

  | std::out_of_range | If n is an invalid index.

This function provides for safer data access. The parameter is first checked that it is in the range of the vector. The function throws out_of_range if the check fails.

Definition at line 552 of file stl_vector.h.

reference at | ( | size_type | __n | ) | `[inline]`

Provides access to the data contained in the vector.

Parameters:

  | n | The index of the element for which data should be accessed.

Returns:
  Read/write reference to data.

Exceptions:

  | std::out_of_range | If n is an invalid index.

This function provides for safer data access. The parameter is first checked that it is in the range of the vector. The function throws out_of_range if the check fails.

Definition at line 534 of file stl_vector.h.

const_reference back | ( |  | ) | const`[inline]`

| Returns a read-only (constant) reference to the data at the last element of the vector. Definition at line 587 of file stl_vector.h.

reference back | ( |  | ) | `[inline]`

| Returns a read/write reference to the data at the last element of the vector. Definition at line 579 of file stl_vector.h.

const_iterator begin | ( |  | ) | const`[inline]`

| Returns a read-only (constant) iterator that points to the first element in the vector. Iteration is done in ordinary element order. Definition at line 342 of file stl_vector.h.

iterator begin | ( |  | ) | `[inline]`

| Returns a read/write iterator that points to the first element in the vector. Iteration is done in ordinary element order. Definition at line 333 of file stl_vector.h. Referenced by vector::insert(), vector::operator=(), std::operator==(), and vector< _Node *, _Nodeptr_Alloc >::vector().

size_type capacity | ( |  | ) | const`[inline]`

| Returns the total number of elements that the vector can hold before needing to allocate more memory. Definition at line 449 of file stl_vector.h. Referenced by vector::operator=().

void clear | ( |  | ) | `[inline]`

| Erases all the elements. Note that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibilty. Definition at line 748 of file stl_vector.h.

bool empty | ( |  | ) | const`[inline]`

| Returns true if the vector is empty. (Thus begin() would equal end().) Definition at line 458 of file stl_vector.h.

const_iterator end | ( |  | ) | const`[inline]`

| Returns a read-only (constant) iterator that points one past the last element in the vector. Iteration is done in ordinary element order. Definition at line 360 of file stl_vector.h.

iterator end | ( |  | ) | `[inline]`

| Returns a read/write iterator that points one past the last element in the vector. Iteration is done in ordinary element order. Definition at line 351 of file stl_vector.h. Referenced by vector::erase(), vector::insert(), vector::operator=(), std::operator==(), and vector< _Node *, _Nodeptr_Alloc >::vector().

vector< _Tp, _Alloc >::iterator erase | ( | iterator | __first,
|  | iterator | __last
| ) |

Remove a range of elements.

Parameters:

  | first | Iterator pointing to the first element to be erased.
  | last | Iterator pointing to one past the last element to be erased.

Returns:
  An iterator pointing to the element pointed to by last prior to erasing (or end()).

This function will erase the elements in the range [first,last) and shorten the vector accordingly.

Note This operation could be expensive and if it is frequently used the user should consider using std::list. The user is also cautioned that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibilty.

Definition at line 122 of file vector.tcc.

References std::copy(), and vector::end().

vector< _Tp, _Alloc >::iterator erase | ( | iterator | __position | ) |

Remove element at given position.

Parameters:

  | position | Iterator pointing to element to be erased.

Returns:
  An iterator pointing to the next element (or end()).

This function will erase the element at the given position and thus shorten the vector by one.

Note This operation could be expensive and if it is frequently used the user should consider using std::list. The user is also cautioned that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibilty.

Definition at line 110 of file vector.tcc.

References std::copy(), and vector::end().

const_reference front | ( |  | ) | const`[inline]`

| Returns a read-only (constant) reference to the data at the first element of the vector. Definition at line 571 of file stl_vector.h.

reference front | ( |  | ) | `[inline]`

| Returns a read/write reference to the data at the first element of the vector. Definition at line 563 of file stl_vector.h.

void insert | ( | iterator | __position,
|  | _InputIterator | __first,
|  | _InputIterator | __last
| ) | `[inline]`

Inserts a range into the vector.

Parameters:

  | position | An iterator into the vector.
  | first | An input iterator.
  | last | An input iterator.

This function will insert copies of the data in the range [first,last) into the vector before the location specified by pos.

Note that this kind of operation could be expensive for a vector and if it is frequently used the user should consider using std::list.

Definition at line 676 of file stl_vector.h.

void insert | ( | iterator | __position,
|  | size_type | __n,
|  | const value_type & | __x
| ) | `[inline]`

Inserts a number of copies of given data into the vector.

Parameters:

  | position | An iterator into the vector.
  | n | Number of elements to be inserted.
  | x | Data to be inserted.

This function will insert a specified number of copies of the given data before the location specified by position.

Note that this kind of operation could be expensive for a vector and if it is frequently used the user should consider using std::list.

Definition at line 657 of file stl_vector.h.

vector< _Tp, _Alloc >::iterator insert | ( | iterator | __position,
|  | const value_type & | __x
| ) |

Inserts given value into vector before specified iterator.

Parameters:

  | position | An iterator into the vector.
  | x | Data to be inserted.

Returns:
  An iterator that points to the inserted data.

This function will insert a copy of the given value before the specified location. Note that this kind of operation could be expensive for a vector and if it is frequently used the user should consider using std::list.

Definition at line 93 of file vector.tcc.

References vector::begin(), and vector::end().

size_type max_size | ( |  | ) | const`[inline]`

| Returns the size() of the largest possible vector. Definition at line 407 of file stl_vector.h.

vector< _Tp, _Alloc > & operator= | ( | const vector< _Tp, _Alloc > & | __x | ) |

Vector assignment operator.

Parameters:

  | x | A vector of identical element and allocator types.

All the elements of x are copied, but any extra memory in x (for fast expansion) will not be copied. Unlike the copy constructor, the allocator object is not copied.

Definition at line 133 of file vector.tcc.

References vector::begin(), vector::capacity(), std::copy(), vector::end(), and vector::size().

const_reference operator[] | ( | size_type | __n | ) | const`[inline]`

Subscript access to the data contained in the vector.

Parameters:

  | n | The index of the element for which data should be accessed.

Returns:
  Read-only (constant) reference to data.

This operator allows for easy, array-style, data access. Note that data access with this operator is unchecked and out_of_range lookups are not defined. (For checked lookups see at().)

Definition at line 509 of file stl_vector.h.

reference operator[] | ( | size_type | __n | ) | `[inline]`

Subscript access to the data contained in the vector.

Parameters:

  | n | The index of the element for which data should be accessed.

Returns:
  Read/write reference to data.

This operator allows for easy, array-style, data access. Note that data access with this operator is unchecked and out_of_range lookups are not defined. (For checked lookups see at().)

Definition at line 494 of file stl_vector.h.

void pop_back | ( |  | ) | `[inline]`

| Removes last element. This is a typical stack operation. It shrinks the vector by one. Note that no data is returned, and if the last element's data is needed, it should be retrieved before pop_back() is called. Definition at line 623 of file stl_vector.h.

void push_back | ( | const value_type & | __x | ) | `[inline]`

Add data to the end of the vector.

Parameters:

  | x | Data to be added.

This is a typical stack operation. The function creates an element at the end of the vector and assigns the given data to it. Due to the nature of a vector this operation can be done in constant time if the vector has preallocated space available.

Definition at line 602 of file stl_vector.h.

const_reverse_iterator rbegin | ( |  | ) | const`[inline]`

| Returns a read-only (constant) reverse iterator that points to the last element in the vector. Iteration is done in reverse element order. Definition at line 378 of file stl_vector.h.

reverse_iterator rbegin | ( |  | ) | `[inline]`

| Returns a read/write reverse iterator that points to the last element in the vector. Iteration is done in reverse element order. Definition at line 369 of file stl_vector.h.

const_reverse_iterator rend | ( |  | ) | const`[inline]`

| Returns a read-only (constant) reverse iterator that points to one before the first element in the vector. Iteration is done in reverse element order. Definition at line 396 of file stl_vector.h.

reverse_iterator rend | ( |  | ) | `[inline]`

| Returns a read/write reverse iterator that points to one before the first element in the vector. Iteration is done in reverse element order. Definition at line 387 of file stl_vector.h.

void reserve | ( | size_type | __n | ) |

Attempt to preallocate enough memory for specified number of elements.

Parameters:

  | n | Number of elements required.

Exceptions:

  | std::length_error | If n exceeds `max_size()`.

This function attempts to reserve enough memory for the vector to hold the specified number of elements. If the number requested is more than max_size(), length_error is thrown.

The advantage of this function is that if optimal code is a necessity and the user can determine the number of elements that will be required, the user can reserve the memory in advance, and thus prevent a possible reallocation of memory and copying of vector data.

Definition at line 69 of file vector.tcc.

References vector::size().

void resize | ( | size_type | __new_size | ) | `[inline]`

Resizes the vector to the specified number of elements.

Parameters:

  | new_size | Number of elements the vector should contain.

This function will resize the vector to the specified number of elements. If the number is smaller than the vector's current size the vector is truncated, otherwise the vector is extended and new elements are default-constructed.

Definition at line 441 of file stl_vector.h.

void resize | ( | size_type | __new_size,
|  | const value_type & | __x
| ) | `[inline]`

Resizes the vector to the specified number of elements.

Parameters:

  | new_size | Number of elements the vector should contain.
  | x | Data with which new elements should be populated.

This function will resize the vector to the specified number of elements. If the number is smaller than the vector's current size the vector is truncated, otherwise the vector is extended and new elements are populated with given data.

Definition at line 422 of file stl_vector.h.

size_type size | ( |  | ) | const`[inline]`

| Returns the number of elements in the vector. Definition at line 402 of file stl_vector.h. Referenced by vector::operator=(), std::operator==(), and vector::reserve().

void swap | ( | vector< _Tp, _Alloc > & | __x | ) | `[inline]`

Swaps data with another vector.

Parameters:

  | x | A vector of the same element and allocator types.

This exchanges the elements between two vectors in constant time. (Three pointers, so it should be quite fast.) Note that the global std::swap() function is specialized such that std::swap(v1,v2) will feed to this function.

Definition at line 733 of file stl_vector.h.

Referenced by std::swap().

The documentation for this class was generated from the following files:

- stl_vector.h
- vector.tcc