 GeeksforGeeks App
Open App Browser
Continue

# 2D Vector of Pairs in C++ with Examples

What is Vector?

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).

What is 2D vector?

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.

What is Pair?

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.

How to access a Pair?

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

Syntax:

auto fistElement = myPair.first;

auto fistElement = myPair.second;

2D Vector of Pairs

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.

## C++

 `// C++ program to demonstrate the``// working of vector of vectors``// of pairs``#include ``using` `namespace` `std;` `// Function to print 2D vector elements``void` `print(vector>> &myContainer)``{``  ``// Iterating over 2D vector elements``  ``for``(``auto` `currentVector: myContainer)``  ``{``    ``// Each element of the 2D vector``    ``// is a vector itself``    ``vector> myVector =``           ``currentVector;` `    ``// Iterating over the vector``    ``// elements``    ``cout << ``"[  "``;``    ``for``(``auto` `pr: myVector)``    ``{``      ``// Print the element``      ``cout << ``"{"``;``      ``cout << pr.first << ``" , "` `<<``              ``pr.second;``      ``cout << ``"}  "``;``    ``}` `    ``cout << ``"]\n"``;``  ``}``}` `// Driver code``int` `main()``{``  ``// Declaring a 2D vector of pairs``  ``vector>> myContainer;` `  ``// Initializing vectors of pairs``  ``// Pairs are of type {int, string}``  ``vector> vect1 =``  ``{{0, ``"GeeksforGeeks"``}, {1, ``"GFG"``},``   ``{2, ``"Computer Science"``}};``  ``vector> vect2 =``  ``{{0, ``"C#"``}, {1, ``"C"``}, {2, ``"C++"``}};``  ``vector> vect3 =``  ``{{0, ``"HTML"``}, {1, ``"CSS"``},``   ``{2, ``"Javascript"``}};``  ``vector> vect4 =``  ``{{0, ``"R"``}, {1, ``"Swift"``},``   ``{2, ``"Python"``}};` `  ``// Inserting vectors in the``  ``// 2D vector``  ``myContainer.push_back(vect1);``  ``myContainer.push_back(vect2);``  ``myContainer.push_back(vect3);``  ``myContainer.push_back(vect4);` `  ``// Calling print function``  ``print(myContainer);` `  ``return` `0;``}`

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.

## C++

 `// C++ program to demonstrate the``// working of vector of vectors``// of pairs``#include ``using` `namespace` `std;` `// Function to print 2D vector elements``void` `print(vector>> &myContainer)``{``  ``// Iterating over 2D vector elements``  ``for``(``auto` `currentVector: myContainer)``  ``{``    ``// Each element of the 2D vector is``    ``// a vector itself``    ``vector> myVector =``           ``currentVector;` `    ``// Iterating over the vector``    ``// elements``    ``cout << ``"[  "``;``    ``for``(``auto` `pr: myVector)``    ``{``      ``// Print the element``      ``cout << ``"{"``;``      ``cout << pr.first << ``" , "` `<<``              ``pr.second;``      ``cout << ``"}  "``;``    ``}` `    ``cout << ``"]\n"``;``  ``}``}` `// Driver code``int` `main()``{``  ``// Declaring a 2D vector of pairs``  ``// Pairs are of type {char, bool}``  ``vector>> myContainer;` `  ``// Initializing vectors of pairs``  ``vector> vect1 =``  ``{{``'G'``, 0}, {``'e'``, 0},``   ``{``'e'``, 0}, {``'k'``, 0}};``  ``vector> vect2 =``  ``{{``'G'``, 1}, {``'e'``, 1},``   ``{``'e'``, 1}, {``'k'``, 1}};``  ``vector> vect3 =``  ``{{``'G'``, 0}, {``'e'``, 0},``   ``{``'e'``, 0}, {``'k'``, 1}};``  ``vector> vect4 =``  ``{{``'G'``, 0}, {``'e'``, 1},``   ``{``'e'``, 0}, {``'k'``, 1}};` `  ``// Inserting vectors in the 2D vector``  ``myContainer.push_back(vect1);``  ``myContainer.push_back(vect2);``  ``myContainer.push_back(vect3);``  ``myContainer.push_back(vect4);` `  ``print(myContainer);` `  ``return` `0;``}`

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)

My Personal Notes arrow_drop_up