VECTOR

 Vector is the most commonly used sequence container class.                                                                As you know, the array in C/C++ has one restriction: its size is fixed at compile time.  The vector class solves this problem by allocating memory as needed.  It defines a dynamic array - an array that can grow during use. The vector provides random access with constant time insertions and deletions at the end.  It supports the standard array-indexing syntax for accessing its elements by overloading [ ] operator. It means that a vector object can be indexed like an array, and you can access an element using the subscript notation.

Vectors are sequence containers of choice when the fastest possible random access is needed, and few if any insertions or deletions are required at any point other than the end.  If many insertions and/or deletions must be done at interior positions, it is better to use a list. If insertions or deletions are needed at the beginning and the end of the sequence, the better choice is a deque.

 To use vector class include <vector>.

 The template specification for vector:                                                                                template <class T, class Allocator=allocator <T> > class vector

where T is  the type of data being stored and Allocator specifies the allocator which defaults to the standard allocator.

 vector has following constructors: 

explicit vector (const Allocator &a=Allocator() );

Constructs an empty vector.

explicit vector (size_type num, const T &val=T(),                                const Allocator &a=Allocator() );

Constructs a vector that has num elements with value val

vector (const vector <T, Allocator> &ob);

vector’s copy constructor.

template <class InIter> vector (InIter start, InIter end,                                          const Allocator &a=Allocator() );

Constructs a vector that contains the elements in the range specified by start and end.

 Although vectors allow array-style indexing, their elements can be also accessed through an iterator. The iterator is defined by container classes. To obtain an iterator for vector:                                vector <T>::iterator i; 

The vector class overloads the assignment operator =. It also defines comparison operators:            ==, <, <=, !=, >, >=      

Some of the most commonly used member functions are: 

Member

Description

iterator begin ();

Returns an iterator to the first element in the vector.

iterator end ();

Returns an iterator to the end of the vector.

bool empty ();

Returns true if the vector contains no elements.

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 range start to end. Returns an iterator to the element after the one removed.

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,

                   const T &val);

Inserts num copies of val immediately before the element specified by i.

template <class InIter>

void insert (iterator i, InIter start, InIter end);

Inserts the sequence defined by start and end immediately before 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.

size_type size () const;

Returns the number of elements currently in the vector.

  !Deletion of the first element can be easily programmed on vector using method v.erase(v.begin()), but that kind of operation should be avoided because it is very inefficient for long vectors – since all of the elements after the first position must be shifted over to fill the hole, it is a linear time operation.

 Example:

#include <vector>
#include <string>
#include <algorithm>
#include <assert.h>
#include <iostream>
using namespace std;

template<class T>          // template function to print a vector of type T
void print(vector<T> v) {
     for(int i = 0; i < v.size(); i++)
       cout<<v[i]<<"\t";
     cout<<endl;
}

main() {
     vector<int> vi;
     vector<string> vs;

     for(int i = 0; i < 20; i++)
       vi.push_back(i);
     assert(!vi.empty());
     print(vi);

     cout<<"Changing values of elements in range 3 through 7...\n";
     for(i = 3; i < 8; i++)
       vi[i] = 100;
     print(vi);

     cout<<"Removing elements in range 3 through 7...\n";
     vi.erase(&vi[3], &vi[8]);
     print(vi);

     vi.clear();                               
     assert(vi.empty());
     cout<<endl;

     assert(vs.empty());
     vs.push_back("John");
     vs.push_back("Tom");
     vs.push_back("Jane");
     vs.push_back("Anna");
     assert(!vs.empty());
     print(vs);

     cout<<"\nSorting elements...\n";
     sort(vs.begin(), vs.end()); // sorts elements in alphabetical order
     print(vs);

     cout<<"\nRemoving last element...\n";
     vs.pop_back();              
     print(vs);
    return 0;
}