VOOZH about

URL: https://www.geeksforgeeks.org/cpp/2d-vector-of-pairs-in-c-with-examples/

⇱ 2D Vector of Pairs in C++ with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

2D Vector of Pairs in C++ with Examples

Last Updated : 23 Jul, 2025

In C++, a vector is similar to dynamic arrays with the ability to resize itself automatically. Vector elements are stored in contiguous memory locations so that they can be accessed and traversed using iterators.

Some of the functions associated with a vector:

  • begin(): Returns an iterator pointing to the first element in the vector. Complexity-O(1).
  • end(): Returns an iterator pointing to the theoretical element that follows the last element in the vector. Complexity-O(1).
  • rbegin(): Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first element. Complexity-O(1).
  • size(): Returns the number of elements in the vector. Complexity-O(1).
  • empty(): Returns whether the vector is empty. Complexity-O(1).
  • push_back(): It pushes the elements into a vector from the back. Complexity-O(1).
  • pop_back(): It is used to pop or remove elements from a vector from the back. Complexity-O(1).
  • insert(): It inserts new elements before the element at the specified position. Complexity-O(1).

In C++, a 2D vector is a vector of vectors which means that each element of a 2D vector is a vector itself. It is the same as a matrix implemented with the help of vectors.

Some of the functions associated with a 2D vector:

  • size(): Returns the number of elements in the 2D vector. 
  • empty(): Returns whether the 2D vector is empty.
  • push_back(): It pushes a vector into a 2D vector from the back.
  • pop_back(): It is used to pop or remove elements from a 2D vector from the back.

Utility header in C++ provides us pair container. A pair consists of two data elements or objects.

  • The first element is referenced as ‘first’ and the second element as ‘second’ and the order is fixed (first, second).
  • Pair is used to combine together two values that may be different in type. Pair provides a way to store two heterogeneous objects as a single unit.
  • Pair can be assigned, copied, and compared. The array of objects allocated in a map or hash_map is of type ‘pair’ by default in which all the ‘first’ elements are unique keys associated with their ‘second’ value objects.
  • To access the elements, we use variable name followed by dot operator followed by the keyword first or second.

To access elements of a pair use the dot (.) operator.

Syntax:

auto fistElement = myPair.first;

auto fistElement = myPair.second;

A 2D vector of pairs or vector of vectors of pairs is a vector in which each element is a vector of pairs itself. 

Syntax:

vector<vector<pair<dataType1, dataType2>> myContainer

Here,

dataType1 and dataType2 can be similar or dissimilar data types.

Example 1: In the below C++ program, a vector of vectors of pairs of type {int, string} is used.


Output:

[  {0 , GeeksforGeeks}  {1 , GFG}  {2 , Computer Science}  ]
[  {0 , C#}  {1 , C}  {2 , C++}  ]
[  {0 , HTML}  {1 , CSS}  {2 , Javascript}  ]
[  {0 , R}  {1 , Swift}  {2 , Python}  ]

Example 2: In the below C++ program, a vector of vectors of pairs of type {char, bool} is used.


Output:

[  {G , 0}  {e , 0}  {e , 0}  {k , 0}  ]
[  {G , 1}  {e , 1}  {e , 1}  {k , 1}  ]
[  {G , 0}  {e , 0}  {e , 0}  {k , 1}  ]
[  {G , 0}  {e , 1}  {e , 0}  {k , 1}  ]

Time Complexity: O(N*M) where N is the size of the vector and M is the size of the pair.

Auxiliary Space: O(M)

Comment
Article Tags: