Home C++ Programming Tutorials Vectors in C++ STL

Vectors in C++ STL

by anupmaurya
98 minutes read

What is vector in C++?

Vector is a dynamic contiguous array and it is a sequence container(linear fashion).

  • In vector we insert as many elements we want to add but in a array  of 10 integer to add the 11th element we need to make an array again.
  • It is likely to array but array has static data type and vector has dynamic datatype.
  • Vector is declared in the header file (#include<vector>).
  • We use vector in C++ because it is dynamic and it automatically adjust the size when an element is inserted or deleted from it.

C++ Vector Declaration

vector<object_type> variable_name; // vector declaration

//Examples
vector<int> number;
vector<string> name;
vector <float> marks;

C++ Vector Initialization

There are different ways to initialize a vector in C++.

//initializing the vector by providing values directly to the vector.
vector<int> vector1= {1, 2, 3, 4, 5};   // first way

//vector with size 5 and initializes the vector with the value of 10
vector<int> vector2(5, 10);  //second way which is equivalent to <int> vector3 = {10, 10, 10, 10, 10};

Example of C++ Vector Initialization

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

int main() {

  // method 1
  vector<int> vector1 = {1, 2, 3, 4, 5};

  // method 2
  vector<int> vector2(5, 10);

  cout << "vector1 = ";

 // ranged loop, which is mostly used to retrieve the elements of vectors
  for (int i : vector1) {
    cout << i << "  ";
  }

  cout << "\nvector2 = ";

  // ranged loop
  for (int i : vector2) {
    cout << i << "  ";
  }

  return 0;
}

Output

vector1 = 1  2  3  4  5  
vector2 = 10  10  10  10  10  

Most used functions in Vector

  • push_back() – it accepts a parameter and insert the element passed in the parameter in the vectors, the element is inserted at the end.
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
  • pop_back() – it deletes the last element and returns it to the calling function.
v1.pop_back();
  • begin() – it returns an iterator pointing to the first element of the vector.
auto iterator = itr;
itr = v1.begin();
  • end() – it returns an iterator pointing to the element theoretically after the last element of the vector.
auto iterator = itr;
itr = v1.end();
  • insert() – it is used to insert an element at a specified position.
auto it= v1.begin();
v1.insert(it,5); //inserting 5 at the beginning
  • erase() – it is used to delete a specific element
vector<int> v1;
auto it= v1.begin();
v1.erase(it);// erasing the first element
  • front() – it returns a reference to the first element of the vector.
v1.front();
  • back() – it returns a reference to the last element of the vector.
v1.back();
  • clear() – deletes all the elements from the vector.
v1.clear();
  • empty() – to check if the vector is empty or not.
v1.empty();
  • size() – returns the size of the vector
v1.size();

Example of Vectors in C++

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

int main() {
  vector < int > v;

  for (int i = 0; i < 10; i++) {
    v.push_back(i); //inserting elements in the vector
  }


  cout << "the elements in the vector: ";
  for (auto it = v.begin(); it != v.end(); it++)
    cout << * it << " ";   // the elements in the vector: 0 1 2 3 4 5 6 7 8 9 

  cout << "\nThe front element of the vector: " << v.front();  //the front element of the vector: 0
  cout << "\nThe last element of the vector: " << v.back();    // the last element of the vector: 9
  cout << "\nThe size of the vector: " << v.size();   // the size of the vector: 10
  cout << "\nDeleting element from the end: " << v[v.size() - 1];  //deleting element from the end: 9
  v.pop_back();

  cout << "\nPrinting the vector after removing the last element:" << endl;
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << " ";

//printing the vector after removing the last element: 0 1 2 3 4 5 6 7 8 

  cout << "\nInserting 5 at the beginning:" << endl;
  v.insert(v.begin(), 5);
  cout << "The first element is: " << v[0] << endl;  //the first element is: 5
  cout << "Erasing the first element" << endl;
  v.erase(v.begin());
  cout << "Now the first element is: " << v[0] << endl;  //now the first element is: 0

  if (v.empty())
    cout << "\nvector is empty";
  else
    cout << "\nvector is not empty" << endl;

  v.clear();
  cout << "Size of the vector after clearing the vector: " << v.size();  //size of the vector after clearing the vector: 0

}
Output:

the elements in the vector: 0 1 2 3 4 5 6 7 8 9
The front element of the vector: 0
The last element of the vector: 9
The size of the vector: 10
Deleting element from the end: 9
Printing the vector after removing the last element:
0 1 2 3 4 5 6 7 8
Inserting 5 at the beginning:
The first element is: 5
Erasing the first element
Now the first element is: 0
vector is not empty
Size of the vector after clearing the vector: 0

Others Function used as Vectors in C++

  • assign() – It assigns new value to the vector elements by replacing old ones
  • swap() – It is used to swap the contents of one vector with another vector of same type. Sizes may differ.
  • clear() – It is used to remove all the elements of the vector container
  • emplace() – It extends the container by inserting new element at position
  • emplace_back() – It is used to insert a new element into the vector container, the new element is added to the end of the vector
  • rbegin() – Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first element
  • rend() – Returns a reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end)
  • cbegin() – Returns a constant iterator pointing to the first element in the vector.
  • cend() – Returns a constant iterator pointing to the theoretical element that follows the last element in the vector.
  • crbegin() – Returns a constant reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first element
  • crend() – Returns a constant reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end)

Time complexity for various operations on vectors

  • Random access – constant O(1)
  • Insertion or removal of elements at the end – constant O(1)
  • Insertion or removal of elements – linear in the distance to the end of the vector O(N)
  • Knowing the size – constant O(1)
  • Resizing the vector- Linear O(N)

Why do we need vectors in C++?

In C++, vectors are used to store elements of similar data types. However, unlike arrays, the size of a vector can grow dynamically. That is, we can change the size of the vector during the execution of a program as per our requirements. Vectors are part of the C++ Standard Template Library.

Why are vectors better than array C++?

Vector is better for frequent insertion and deletion, whereas Arrays are much better suited for frequent access of elements scenario. Vector occupies much more memory in exchange for managing storage and growing dynamically, whereas Arrays are a memory-efficient data structure.

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.