Horje
How to Create a Vector of Vectors of Pairs in C++?

In C++ STL, we can nest different containers into each other at any number of levels. On such container is the Vector of Vectors of Pairs i.e. 2D vector of pairs.

In this article, we will learn how to create and traverse a vector of vector of pairs in C++ STL.

Vector of Vectors of Pairs in C++

The 2d vector of pairs can be created by defining the type of the nested vector as pairs. We will first define the type of the most outer vector as another vector. Then the type of each nested vector will be a std::pair.

This hybrid container provides the following time complexity for basic operators:

  • Traverse: O(N * M)
  • Search: O(N * M)
  • Insertion at Back: O(N)
  • Insertion in Middle: O(N * M)
  • Deletion from Back: O(N)
  • Deletion in Middle: O(N * M)

Based on the above performance, we can decide whether to use this or use other containers. This container type can be useful when the order does not matter and we only need to insert and delete at the end and also need random access.

C++ Program To Create a Vector of Vectors of Pairs

C++

// C++ Program To Create a Vector of Vectors of Pairs
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
  
int main()
{
    // Declare a vector of vectors of pairs.
    // Pairs are the type of (int,int).
    vector<vector<pair<int, int> > > vvp;
  
    // Initialize 3 vectors of pairs of type (int,int);
    vector<pair<int, int> > v1
        = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
    vector<pair<int, int> > v2 = { { 7, 8 }, { 9, 10 } };
    vector<pair<int, int> > v3 = { { 11, 12 } };
  
    // Insert these vectors of pairs into the vector of
    // vectors of pairs.
    vvp.push_back(v1);
    vvp.push_back(v2);
    vvp.push_back(v3);
  
    // print the vector of vectors of pairs using iterator.
    cout << "2D Vector of Pairs:" << endl;
    cout << "{" << endl;
    for (vector<pair<int, int> >& it : vvp) {
        cout << "{";
        for (pair<int, int>& p : it) {
            cout << "{" << p.first << ", " << p.second
                 << "}";
        }
        cout << "}" << endl;
    }
    cout << "}" << endl;
  
    return 0;
}

Output

2D Vector of Pairs:
{
{{1, 2}{3, 4}{5, 6}}
{{7, 8}{9, 10}}
{{11, 12}}
}

Time Complexity: O(N * M), where N is the number of nested vectors, and M is the average number of pairs in each nested vector.
Auxiliary Space: O(N * M)




Reffered: https://www.geeksforgeeks.org


C++

Related
How to Access Vector Element Using Iterator in C++? How to Access Vector Element Using Iterator in C++?
How to Increase the Capacity of a Vector in C++? How to Increase the Capacity of a Vector in C++?
Find All Occurrences of an Element in a Vector Find All Occurrences of an Element in a Vector
How to Find the Size of a Vector in C++? How to Find the Size of a Vector in C++?
How to Find a Pair by Key in a Vector of Pairs in C++? How to Find a Pair by Key in a Vector of Pairs in C++?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12