A random access dynamic container

You're viewing an older version of this page (#3318). View the current version.

View versions (3)

Name

vector

Declaration

#include<vector>
namespace std {
  tempate <class T, class Allocator=allocator<T>>
  class vector;
}

Description

A vector manages a dynamic array whose elements are of any type T that are both assignable and copyable.

A simple example of creating a vector is:

std::vector<int> a;      // creates an empty vector of integer type
  std::vector<float> b(10);  // crease a vector with 10 float types.

With access to the vector contains performed in the usual C way:

std::cout<<a[0]<<std::endl;
  std::cout<<b[2]<<std::endl;

The optional second parameter defines the memory model, which by default is an allocator.

Performance

Vectors copy their elements into their internal dynamic array, whose elements are ordered. Vectors provide constant time random access to all of the data. The iterators are also random access iterators, therefore vectors can be used with any algorithm within STL.

Vectors provide food performance if you append (or delete) elements from the end of the container. If you insert or delete from any other position then the performance is poor, with every element after the insertion or deletion point being moved to maintain an ordered and continuous array of elements.

Size and Capacity

One way vectors achieve good performance is to allocate more memory than then actually need for the purpose.

To avoid reallocation you can use the reserve command when you instantiate the container, i.e.

std::vector<int> v;
v.reserve(80);    // reserve memory for 80 elements

this is equivalent to

std::vector<T> v(80);

Vector Operations

<b>Create, Copy and Destroy Operations</b>

//! Creates an empty vector without any elements
vector<Elem> c
//! Creates a copy of another vector of the same type
vector<Elem> c1(c2)
//! Creates a vector with \a n elements that are created using the default contructor of \a Elem
vector<Elem> c(n)
//! Creates a vector initialised with \a n copies of element \a elem
vector<Elem> c(n,elem)
//! Creates a vector initialised with the elements from another container, defined by its iterators (\a begin, \a end)
vector<Elem> c(begin, end)
//! Destroys all elements and frees the memory
void c.~vector<Elem>()

<b>Nonmodifying Operations</b>

//! Returns the actual number of elements in the container
int c.size()
//! Returns if the container is empty
bool c.empty()
//! Returns the maximum number of possible elements
int c.max_size()
//! Returns the maximum possible number of elements without memory reallocation
int capacity()
//! Enlarges the current max \e capacity of the container to contain a maximum of \a a elements
void c.reserve(int a)
//! Returns if \a c1 is equal to \a c2
bool c1==c2
//! Returns if \a c1 is not equal to \a c2
bool c1!=c2
//! Returns if \a c1 is less than \a c2
bool c1<c2
//! Returns if c1 is greater than c2
bool c1>c2
//! Returns if \a c1 is less than or equal to \a c2
bool c1<=c2
//! Returns if \a c1 is greater than or equal to \a c2
bool c1>=c2

<b>Assignments</b>

//! Assigns all elements of \a c2 to \a c1
void c1=c2
//! Assigns \a n copies of element \a elem
void c.assign(b,elem)
//! Assigns the elements of the range [\a begin, \a end]
void c.assign(begin,end)
//! Swaps the data of \a c1 and \a c2
void c1.swap(c2)
void swap(c1,c2)