Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

C++ STL: vector (Complete Guide)

Divya Jain
Divya Jain

Vectors are sequential containers that are very similar to arrays. They are continuous blocks of memory. Unlike arrays, vectors can have dynamic sizes. Vectors are one of the most useful containers in STL. Vector is present in #include<vector>header file. The elements inside the vector can be accessed using iterators. 

Vector Declaration

Syntax:

vector<data_type> name(size,initial_value);

size: optional parameter which tells us the size of the vector

initial_value: optional parameter which initializes the vector with the given value

Example:

vector < int > v; //initializes a vector of size 0 which stores integer values 
vector < int > v(10); //initializes a vector of size 10 having initial value as 0
vector < int > v(10, 1); //initializes a vector of size 10 having initial value as 1

Functions on vectors

  • push_back(element): It pushes the value at the end of the vector. 
    Time Complexity: O(1) 
    Parameters: the element which is to be inserted
    Return type: void
     
  • size(): It tells us the size of the vector.
    Time Complexity: O(1)
    Parameters: None
    Return type: integer - total number of elements in the vector
     
  • pop_back(): It deletes the last element of the vector. 
    Time Complexity: O(1)
    Parameters: None
    Return type: void
     
  • clear(): It deletes all the elements from the vector.
    Time Complexity: O(N) where N is the size of the vector
    Parameters: None
    Return type: void
     
  • empty(): It tells us whether the vector is empty or not.
    Time Complexity: O(1)
    Parameters: None
    Return type: Boolean, true if vector is empty else false
#include<iostream>
#include<vector>
using namespace std;

void print(vector < int > v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << '\n';
  return;
}
int main() {
  vector < int > v;
  for (int i = 0; i < 5; i++) {
    v.push_back(i + 1);
  }
  cout << "Size of vector is " << v.size() << '\n';
  print(v);
  v.pop_back();
  print(v);
  v.clear();
  if (v.empty() == true) {
    cout << "vector is empty!";
  }
  return 0;
}

Output

Size of vector is 5
1 2 3 4 5
1 2 3 4
vector is empty!

Copying one vector to another

vector < int > v2 = v1; //copies vector1 into vector2	

Any change in v2 will not affect v1. The time complexity of this operation is O(N) where N is the size of the vector1. The same can be done using reference but it will not create a copy, it will contain a reference to the original vector.

vector < int > & v2 = v1; //v2 references v1

Passing vector by value to a function

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

void print(vector < int > v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  return;
}
int main() {
  int n;
  cin >> n;
  vector < int > v(n);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  print(v);
  return 0;
}

When the vector v is passed to the print function from the main function, a copy is created which takes O(N) time.

Passing vector by reference to a function

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

void print(vector < int > & v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  return;
}
int main() {
  int n;
  cin >> n;
  vector < int > v(n);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  print(v);
  return 0;
}

When the vector v is passed to the print function from the main function, the reference to vector v is passed. This prevents us from creating a copy of the vector.

Nesting in vectors

Array of vectors

vector < int > arr[10]; //an array arr whose each element is a vector

An array of vectors behaves like a 2D array but it has variable size columns.

vector of vectors

vector < vector < int >> v; //a vector v whose each element is a vector

vector of vectors behaves like a 2D array but it has variable size rows and columns.

Divya Jain
Divya Jain
Divya is an incoming SDE at Atlassian. She loves solving problems and web development. She loves to explore new things and is up for interesting conversations about tech.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet