A random access dynamic container

You're viewing an older version of this page (#3347). 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 is a container that holds object of any type T. It should be noted that these objects are stored sequentially. Accessing individual objects in the container and appending objects in the container is achieved in constant time, whereas finding a specific object and inserting objects in the container is achieved in linear time.

The following pseudo-code illustrates the creation of a vector:

std::vector<int> emptyvector; // creates an empty vector of integer type
  std::vector<char> characters(10);  // crease a vector with 10 characters.

Accessing individual object in the container is done the same way like array. Note that indices in the container start at 0 not 1.

std::cout<<a[0]<<std::endl; //Accessing the first element of the container
  std::cout<<b[2]<<std::endl; // Accessing the third element of the container

The second optional parameter is the type of the allocator object used to define the storage allocation model. By default, the allocator class template for type T is used, which defines the simplest memory allocation model and is value-independent.

Performance

As mentioned early, accessing objects in the container is achieved in constant time. Overflowing the default allocated memory space can be very expensive. This is because the container starts by grabbing a random amount of chunk of memory. If every object on the container fits in that chunk of memory, then insertions and deletions would be done rapidly. (If you do know exactly how many objects you would be using in the container, you can book the corresponding storage using reserve method). However, if you overflow that default memory space, then the container will need to request a bigger memory space and move all its objects into that new space. This operation can be very expensive. The next section will talk about size and capacity of the vector.

Size and Capacity

In order to achieve good performance, you should book in advance space for the container by using the following methods: reserve

std::vector<int> myvector;
myvector.reserve(100);    // reserve memory for 100 objects

this is equivalent to

std::vector<T> myvector(100);

but with the former approach, if you overflow the container, there is no perfomance drawback.

Vector Operations

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

//! This is the default constructor: initialises an empty container without any objects
vector<Elem> v
//! Creates an empty container c1 by calling the default constructor and calls the copy constructor of the vector class to copy the contain of c2 into c1
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> Iterators </b>

//! Returns an \b iterator referring to the first element in the vector container.
iterator begin()
const_iterator begin () const;
//! Returns an iterator referring to the past-the-end element in the vector container.
 iterator end ();
const_iterator end () const;
//! Return reverse iterator to reverse beginning
 reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
//! Return reverse iterator to reverse end
//!Returns a reverse iterator referring to the element right before the first element in the vector, which is considered its reverse end.
 reverse_iterator rend();
const_reverse_iterator rend() const;

Returns a reverse iterator referring to the last element in the vector container. <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)