Open In App

2D Vector of Pairs in C++ with Examples

Last Updated : 28 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 <bits/stdc++.h>
using namespace std;
 
// Function to print 2D vector elements
void 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 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<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 elements
void 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 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<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}  ]

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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads