What Is Vectors, Benefits Of Using Vectors, Cpp Code On Vectors

what is vectors

Hello guys, welcome back to my blog. In this article, I will discuss what is vectors, the benefits of using vectors, Cpp code on_vectors, the different types of functions frequently used in arrays, etc.

If you have any doubts related to electrical, electronics, and computer science, then ask questions. You can also catch me on Instagram – Chetan Shidling. 

Also, read:

What Is Vectors

Before proceding to the discussion of vectors, let us see “what are the limitations of arrays?” Even though the data can be accessed very fast using arrays, there are some limitations. The various limitations of the array are:

01. Once an array is created, its size remains fixed. This means that only a fixed number of elements can be stored in the array.

02. Inserting an element in the middle of the array is time consuming.

03. Deleting an element from the middle of the array is time consuming.

All the above disadvantages are can be overcome using dynamic arrays. The dynamic arrays in C++ can be used using vectors. Now, let us see “What is a vector in C++”.

A vector in C++ is a class that supports dynamic array. Using dynamic array the size of the array can grow or shrink as needed. Even though vector is dynamic, we can use standard array subscript notation to access the various elements. Two vectors can be compared using the following relations.

==, <, <=, >, >=, !=

A vector (or dynamic array) in C++ can be declared first by including the header file <vector.h>. When we declare a vector object, we must specify the type of the element the vector object stores. The various ways to declare vector objects are shown below:

vector<int> i;         //creates a dynamic array of 0 int elements

vector<char>c(5);      //creates a dynamic array of 5 char elements

vector<char> c(5,'x'); //creates a dynamic array of 5 char elements and initizlize all 5 locations with character 'x'

vector<int> a(i);      //create an int vector from already existing int vector

All the variable declared using the vector type are called vector containers or vector or vector object or object. In the above declaration, i, c, and a are called vectors or vector objects.

Now, let us see “What are the frequenctly used member functions defined by vector?” If a is declared as a vector, then various member functions can be accessed. The method of accessing the member functions and the description of each member function is shown below.

a.front(); // access front() and returns the first element in the vector.

a.back(); // access back() and returns the last element in the vector.

a.at(i); // access at() and returns an element at the position specified by i.

a[i] // Returns an element at the position specified by i.

a.size(); // access size() and returns the size of a vector.

a.empty(); // access empty() and returns to true if vector a is empty, false otherwise.

a.begin(); // access begin() and returns an iterator to the first element in the vector.

a.end(); // access end() and returns an iterator to the end of the vector.

a.clear(); // access clear() and removes all elements from the vector.

a.push_back(v); //access push_back() and insert v at the end of the vector.

a.pop_back(); //access pop_back() and removes the last element in the vector.

a.erase(i); //access and removes an element at position i and returns an iterator to the element after the one removed.

a.erase(i,j); //access erase() and removes all the elements in vector in the range i to j.

a.insert(i, val); //access insert() and inserts val at the position i.

a.insert(i, num, val); //access insert() and inserts num copies of val from position i.

Vectors are nothing but arrays. But, dynamic arrays. Now, let us “Write a C++ program to create a vector, read elements to the vector, add elements to the vector and modify vector elements” The program along with output is shown below:

Program to demonstrate creation and manipulation of vectors

#include <iostream.h>
#include <vector.h>

void main()
{
     vector <int> a;    //creates an array with 0 elements
     int i;
     a.push_back(10);   //Insert 10
     a.push_back(20);   //Insert 20
     a.push_back(30);   //Insert 30
     a.push_back(40);   //Insert 40

     cout<<"The vector elements are "<<endl;
     for(i = 0; i < a.size(); i++)
         cout << a[i] << " ";
         cout << endl;

//Output:
// The vector elements are 10 20 30 40


     for(i = 0; i < a.size(); i++)
         a[i] = 2 * a[i];

     cout << "The vector elements are " << endl;
     for(i = 0; i < a.size(); i++)
         cout << a[i] << "";
         cout << endl;

//Output:
// The vector elements are 20 40 60 80
}

The above program is self-explanatory. Now, consider another program that manipulates characters using vectors.

I hope this article “What Is Vectors” may help you all a lot. Thank you for reading.

Also, read:

About The Author

Share Now