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

19 Dec, 2021
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
end(): Returns an iterator pointing to the theoretical element that follows the last element in the vector
rbegin(): Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first element
size(): Returns the number of elements in the vector.
empty(): Returns whether the vector is empty.
push_back(): It pushes the elements into a vector from the back
pop_back(): It is used to pop or remove elements from a vector from the back.
insert(): It inserts new elements before the element at the specified position
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 <bits/stdc++.h>using namespace std;  // Function to print 2D vector elementsvoid print(vector<vector<pair<int,           string>>> &myContainer){  // Iterating over 2D vector elements  for(auto currentVector: myContainer)  {    // Each element of the 2D vector     // is a vector itself    vector<pair<int, string>> myVector =            currentVector;      // Iterating over the the vector     // elements    cout << "[  ";    for(auto pr: myVector)    {      // Print the element      cout << "{";      cout << pr.first << " , " <<               pr.second;      cout << "}  ";    }       cout << "]\n";  }}  // Driver codeint main() {  // Declaring a 2D vector of pairs  vector<vector<pair<int,   string>>> myContainer;    // Initializing vectors of pairs  // Pairs are of type {int, string}  vector<pair<int, string>> vect1 =   {{0, "GeeksforGeeks"}, {1, "GFG"},    {2, "Computer Science"}};  vector<pair<int, string>> vect2 =   {{0, "C#"}, {1, "C"}, {2, "C++"}};  vector<pair<int, string>> vect3 =   {{0, "HTML"}, {1, "CSS"},    {2, "Javascript"}};  vector<pair<int, string>> 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 <bits/stdc++.h>using namespace std;  // Function to print 2D vector elementsvoid print(vector<vector<pair<char,            bool>>> &myContainer){  // Iterating over 2D vector elements  for(auto currentVector: myContainer)  {    // Each element of the 2D vector is     // a vector itself    vector<pair<char, bool>> myVector =            currentVector;      // Iterating over the the vector     // elements    cout << "[  ";    for(auto pr: myVector)    {      // Print the element      cout << "{";      cout << pr.first << " , " <<               pr.second;      cout << "}  ";    }       cout << "]\n";  }}  // Driver codeint main() {  // Declaring a 2D vector of pairs  // Pairs are of type {char, bool}  vector<vector<pair<char, bool>>> myContainer;    // Initializing vectors of pairs  vector<pair<char, bool>> vect1 =   {{'G', 0}, {'e', 0},    {'e', 0}, {'k', 0}};  vector<pair<char, bool>> vect2 =   {{'G', 1}, {'e', 1},    {'e', 1}, {'k', 1}};  vector<pair<char, bool>> vect3 =   {{'G', 0}, {'e', 0},    {'e', 0}, {'k', 1}};  vector<pair<char, bool>> 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}  ]
cpp-pair
cpp-vector
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
std::string class in C++
Inline Functions in C++
Pair in C++ Standard Template Library (STL)
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++
Destructors in C++