Friday, June 29, 2012

Vector Operations in C++ -- Part 1

At the end of this series of posts, you will understand the working of vectors, the functions used in vectors. In addition to this, you will be able to do the basic CRUD operations on the vectors.

Vector is a type of sequence container in C++ and is implemented like a dynamic array. Vectors are very similar to arrays except that the arrays have a constant size while vectors can be extended or contracted as needed. However, the size of vector can also be fixed and resized. Once the storage of a vector is exhausted, it grows automatically in size. The storage space they have been allocated can be either equal or greater than the actual size. The extra amount of storage allocated is not used, but is reserved for the vector to be used in case it grows. Thus, the vector does not have to re-allocate storage on each occasion it grows or a new element is inserted.

Similar to arrays, we can access a given element in a vector easily with the index. The elements of vectors are stored in contiguous memory locations just like in the case of arrays. We can iterate over a vector by using its index or by using iterators used in vectors.

Adding or removing the elements from the vector at the ends is done very well as far as the time complexity is concerned. However, the performance of insertion/deletion at positions other than the ends is worse than that of the other sequence containers in C++ (deques and lists).

Constructors for the vector class are as follows:


Constructor Name Syntax Example Description
Default Constructor vector<T> vector_name; vector<int> a; Creates a vector with no elements
Repetitive sequence Constructor vector<T> vector_name(No. of Elements to be repeated, Value to be assigned); vector<int> b(4,100); Creates a vector of 4 elements with value 100.
Iterator constructor vector<T> vector_name (Beginning of another vector, End of another vector); vector<int> c (a.begin(),a.end()); Creates a vector with elements from vector “a”. Can also create a vector by copying only a part of the vector “a”.
Copy constructor vector<T> vector_name(another_vector); vector<int> d(c); Creates a vector by copying the vector “c” into vector “d”.


Accessing an element in a vector:
An element in a Vector can be accessed in two ways:
1. vector_name[index]     (or)
2. vector_name.at(index)

Iterating over a vector using an iterator:
We can iterate over a vector using a normal for loop with the initialization variable from 0 to the vector's maximum size (vector.size( )). But here is another method to explore all the elements in a vector:

vector<int> vector_name;
vector::iterator it;
for(it=vector_name.begin();it < vector_name.end(); it++)
      cout<<*it;

This piece of code will print all the elements one by one in the vector. Similar to iterator, there is also reverse iterator, which iterates over the vector from right to left.

Functions in Vector:

Function Syntax Description
push_back vector.push_back(element) Inserts the element at the end of the vector
pop_back vector.pop_back() Deletes the last element in the vector and returns the same
erase vector.erase(position) Deletes the element from the vector in the specified position/index
insert vector.insert(element,position) Inserts an element (1st argument) into the position (2nd argument)

In the next post, we will apply this concept of vectors in our case study, which involves the basic CRUD operations.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...