vector in c++

How to Make C++ Vector using STL Explained 2020

Vector in C++

We Already know how to declare an array in our previous post. In this post, we focus on how to declare an array as c++ vector using c++ STL library. Most importantly we discussed the c++ header library to make an array of dynamic sizes in this post.

However, Some of the most commonly used classes for implementing sequential lists or arrays are

Vector

List

Let’s look at vector in this post in details.

Vector

A vector in C++ STL is a class that represents a dynamic array. The advantages of vector over normal arrays are,

  • We do not need to pass size as an extra parameter when we pass a vector.
  • Vectors have many in-built functions for erasing an element, inserting an element, etc.
  • Vectors support dynamic sizes, we do not have to initially specify the size of a vector. However, resizing of vector also be done.
  • There are many other functionalities vector provide.

Vectors are dynamic Array

Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. To clarify, data is inserted at the end. Inserting at the end takes differential time, as sometimes there may be a need of extending the array. Removing the last element takes only constant time because no resizing happens but Inserting and erasing at the beginning or in the middle is linear in time. In short , vectors are purely dynamic array.

To use the Vector class, include the below header file in your program:

#include< vector >


Declaring Vector:

vector< Type_of_element > vector_name;

For example, Type_of_element can be any valid C++ data type,
or can be any other container also like Pair, List etc.

Important Function in Vector

  • begin() – Returns an iterator pointing to the first element in the vector.
  • end() – Returns an iterator pointing to the theoretical element that follows the last element in the vector.
  • size() – Returns the number of elements in the vector.
  • capacity() – Returns the size of the storage space currently allocated to the vector expressed as the number of elements.
  • empty() – Returns whether the container is empty. In other words, it gives you boolean value.
  • push_back() – It pushes the elements into a vector from the back.
  • pop_back() – Poping from the back is done by pop_back function
  • insert() – It inserts new elements before the element at the specified position.
  • erase() – It is used to remove elements from a container from the specified position or range.
  • swap() – It is used to swap the contents of one vector with another vector of the same type and size.
  • clear() – It is used to remove all the elements of the vector container.
  • emplace() – It extends the container by inserting a new element at the position.
  • emplace_back() – It is used to insert a new element into the vector container, but, the new element is added to the end of the vector.



// C++ program to illustrate the above functions

#include <iostream> 
#include <vector> 

using namespace std; 

int main() 
{ 
    vector<int> v; 
    
    // Push elements 
    for (int i = 1; i <= 5; i++) 
        v.push_back(i); 

    cout << "Size : " << v.size(); 
    
    // checks if the vector is empty or not 
    if (v.empty() == false) 
        cout << "\nVector is not empty"; 
    else
        cout << "\nVector is empty"; 

    cout << "\nOutput of begin and end: "; 
    for (auto i = v.begin(); i != v.end(); ++i) 
        cout << *i << " "; 
        
    // inserts at the beginning 
    v.emplace(v.begin(), 5); 
    cout << "\nThe first element is: " << v[0]; 
  
    // Inserts 20 at the end 
    v.emplace_back(20); 
    int n = v.size(); 
    cout << "\nThe last element is: " << v[n - 1]; 
  
    // erases the vector 
    v.clear(); 
    cout << "\nVector size after erase(): " << v.size();     

    return 0; 
}

Output:
Size : 5
Vector is not empty
Output of begin and end: 1 2 3 4 5 
The first element is: 5
The last element is: 20
Vector size after erase(): 0

Example 1 – Try Hand on this Leetcode problem

Sudhanshu is Technology geek and also a pro pubg player. I like to create content in my free time that helps others. Currently pursuing BCA from Noida and operating Geekstocode

1 Comment

Leave a Comment

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