std::vector explained with simple example

Vector in C++ are part of Standard Library Container Classes which basically has the functionality of dynamic array which are flexible in size.
C++ STL provides a template class definition for Vectors which are very efficient, refined, and easy to use.
It also has lots of very useful member functions which can be used directly in programs for faster implementation.

Let’s have a look into std::vector class template definition.

template <class T, class Allocator = allocator<T>> class vector
where T = data type that is being stored and allocator defines the allocator functions.

Initialization

There are multiple ways a vector can be initialized.

  • Initialize an empty vector.
    • Constructor — explicit vector(const Allocator &a = Allocator( ) );
    • Usage – std::vector <int> v;
  • Initialize vector with some values.
    • Constructor — explicit vector(size_type num, const T &val = T ( ), const Allocator &a = Allocator( ));
    • Usage – std::vector <int> v2 = {10, 11, 12, 13, 14};
  • Initialize vector from some other vector.
    • Constructor — vector(const vector<T, Allocator> &ob);
    • Usage — vector <int> v3 (v2);

Member functions

Let’s have a look into the member functions that are available with std::vector class.

Member functionUsage
void assign(size_type num, const T &val);

template< class InputIt >
void assign( InputIt first, InputIt last );
Assigns the “num” elements in vector with value val.

It can also take initializer list to assign values.
reference at(size_type location);
const_reference at(size_type location) const;
Returns the reference of the element present at location.
reference back( );
const_reference back( ) const;
Returns the reference of the last element present in vector.
iterator begin( );
const_iterator begin( ) const;
First element of the vector is returns as an iterator.
iterator end( );
const_iterator end( ) const;
Iterator to the end of vector is returned.
reference front( );
const_reference front( ) const;
Returns the reference to the first element of the vector.
size_type capacity( ) const;Returns the current vector capacity.
void clear( );Clear the vector and all entries will be deleted.
bool empty( ) const;Returns true if vector is empty else false.
iterator erase(iterator i);Removes the element at location i.
iterator erase(iterator start, iterator end);Removes the element from start to end and iterator is returned after removing last element.
iterator insert(iterator i, const T &val);Value “val” is inserted at location i and corresponding iterator is returned.
void insert(iterator i, size_type num, const T & val);Insert “num” copies of val into vector at location i.
template <class InIter> void insert(iterator i, InIter start, InIter end);Insert the sequence defined between start and end at location i.
size_type max_size( ) const;Returns the maximum size that vector can have.
void pop_back( );Removes the last element in the vector.
void push_back(const T &val);Adds the element with value set to “val” at the end of the vector.
reverse_iterator rbegin( ); const_reverse_iterator rbegin( ) const;Reverse iterator starting from the end of the vector is returned.
reverse_iterator rend( ); const_reverse_iterator rend( ) const;Reverse iterator starting from the start of the vector is returned.
void reserve(size_type num);Set the capacity of the vector to minimum value of “num”
void resize(size_type num, T val = T ( ));Changes the size of the vector to the mentioned value “num”. All the new elements which got added would be at the end.
size_type size( ) const;Returns the size of the vector.
void swap(vector<T, Allocator> &ob);Exchange the vector content with passed vector content.
Vector member functions list

Let’s have a look into a sample program which uses above function to showcases its usage.

#include <iostream>
#include <vector>
using namespace std;

void print_vector (vector<int>& v) {
    cout << "Vector contents -- ";
    for (vector<int>::iterator itr = v.begin (); itr != v.end(); itr++) {
        cout << *itr << " ";
    }
    cout << endl;
}

void print_vector_reverse (vector<int>& v) {
    cout << "Vector contents in reverse -- ";
    for (vector<int>::reverse_iterator itr = v.rbegin (); itr != v.rend(); itr++) {
        cout << *itr << " ";
    }
    cout << endl;
}

int main() {
	vector <int> v1;
	vector <int> v2 = {10, 11, 12, 13, 14};
	vector <int> v3 (v2);
	
	cout << "Size: " << v1.size () << " Capacity: " << v1.capacity () << " Max size: " << v1.max_size() << endl;
	cout << "Size: " << v2.size () << " Capacity: " << v2.capacity () << " Max size: " << v2.max_size() << endl;
	cout << "Size: " << v3.size () << " Capacity: " << v3.capacity () << " Max size: " << v3.max_size() << endl;
	
	v1.assign (2, 100);
	print_vector (v1);
	
	v1.assign ({200, 201, 202, 203});
	print_vector (v1);
	
	int& ref = v1.at(2);
	ref = 205;
    int& ref1 = v1.back();
    ref1 = 206;
	print_vector (v1);
	
	v2.clear();
	if (v2.empty()) {
	    cout << "Vector got cleared !! " << endl;
	}
	
    vector<int> v4{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    print_vector(v4);
    v4.erase(v4.begin());
    print_vector(v4);
    v4.erase(v4.begin()+2, v4.begin()+5);
    print_vector(v4);

    for (std::vector<int>::iterator it = v4.begin(); it != v4.end();)
    {
        if (*it % 3 == 0)
            it = v4.erase(it); /* No need to increment iterator explicitly, erase will auto do it */
        else
            ++it;
    }
    print_vector(v4);
    
    int& x = v4.front();
    cout << "Element at front: " << x << endl;
    
    vector<int>::iterator itr = v4.begin() + 2;

    v4.insert (itr, 20);
    v4.insert (v4.end(), 30);
    v4.insert (v4.end() - 3, {40, 41,42});
    print_vector(v4);

    v4.pop_back();
    print_vector(v4);
    v4.push_back(50);
    print_vector(v4);
    print_vector_reverse (v4);
    
    cout << "Size: " << v1.size () << " Capacity: " << v1.capacity () << " Max size: " << v1.max_size() << endl;
	cout << "Size: " << v2.size () << " Capacity: " << v2.capacity () << " Max size: " << v2.max_size() << endl;
	cout << "Size: " << v3.size () << " Capacity: " << v3.capacity () << " Max size: " << v3.max_size() << endl;
	
	v1.reserve(33);
	v2.assign(4, 100);
	print_vector(v2);
	v2.resize(22);
	print_vector(v2);
	print_vector (v3);
	v3.resize(11, 60);
	print_vector (v3);
	
	cout << "Size: " << v1.size () << " Capacity: " << v1.capacity () << " Max size: " << v1.max_size() << endl;
	cout << "Size: " << v2.size () << " Capacity: " << v2.capacity () << " Max size: " << v2.max_size() << endl;
	cout << "Size: " << v3.size () << " Capacity: " << v3.capacity () << " Max size: " << v3.max_size() << endl;

    print_vector(v2);
    print_vector(v3);
    v2.swap(v3);
    print_vector(v2);
    print_vector(v3);
    
	return 0;
}

Let’s look into the output of the above program.

Size: 0 Capacity: 0 Max size: 4611686018427387903
Size: 5 Capacity: 5 Max size: 4611686018427387903
Size: 5 Capacity: 5 Max size: 4611686018427387903
Vector contents -- 100 100 
Vector contents -- 200 201 202 203 
Vector contents -- 200 201 205 206 
Vector got cleared !! 
Vector contents -- 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
Vector contents -- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
Vector contents -- 1 2 6 7 8 9 10 11 12 13 14 15 
Vector contents -- 1 2 7 8 10 11 13 14 
Element at front: 1
Vector contents -- 1 2 20 7 8 10 11 40 41 42 13 14 30 
Vector contents -- 1 2 20 7 8 10 11 40 41 42 13 14 
Vector contents -- 1 2 20 7 8 10 11 40 41 42 13 14 50 
Vector contents in reverse -- 50 14 13 42 41 40 11 10 8 7 20 2 1 
Size: 4 Capacity: 4 Max size: 4611686018427387903
Size: 0 Capacity: 5 Max size: 4611686018427387903
Size: 5 Capacity: 5 Max size: 4611686018427387903
Vector contents -- 100 100 100 100 
Vector contents -- 100 100 100 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Vector contents -- 10 11 12 13 14 
Vector contents -- 10 11 12 13 14 60 60 60 60 60 60 
Size: 4 Capacity: 33 Max size: 4611686018427387903
Size: 22 Capacity: 22 Max size: 4611686018427387903
Size: 11 Capacity: 11 Max size: 4611686018427387903
Vector contents -- 100 100 100 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Vector contents -- 10 11 12 13 14 60 60 60 60 60 60 
Vector contents -- 10 11 12 13 14 60 60 60 60 60 60 
Vector contents -- 100 100 100 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

Leave a Reply

Your email address will not be published. Required fields are marked *