How to Create a Vector of Vectors of Pairs in C++? Last Updated : 06 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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; } Output2D 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) Comment More infoAdvertise with us Next Article How to Create a Vector of Vectors of Pairs in C++? S sourabhcao9e0 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector cpp-pair CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Create a Vector of Pairs in C++? In C++, std::pair is the data type that stores the data as keys and values. On the other hand, std::vector is an STL container that stores the collection of data of similar type in the contiguous memory location. In this article, we will learn how to combine these two to create a vector of pairs in 5 min read How to Create a Set of Vectors in C++? In C++, a set is a type of associative container in which duplicate elements are not allowed and a vector is a dynamic array in which duplicate elements are allowed. In this article, we will learn how to create a set of vectors in C++. For Example, Input:vector<int> vec1={1, 2, 3};vector<in 2 min read How to Convert Map to a Vector of Pairs in C++? In C++, map containers allow us to store the data in key-value pairs. There might be some cases where you want to convert an entire map into a vector of pairs. In this article, we will learn how to convert a map into a vector of pairs. Example Input: map<int,string>mp ={ {1,"one"}, {2,"two"}, 2 min read How to Create a Stack of Vectors in C++? In C++, a stack of vectors can be created using the Standard Template Library (STL). The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure, and a vector is a dynamic array that can grow and shrink in size. In this article, we will learn how to create a stac 2 min read How to Convert a Vector of Pairs to a Map in C++? In C++, a vector of pairs can be converted to a map. This can be useful when you want to create a map from a vector of pairs, where each pair contains a key and a value. In this article, we will learn how to convert a vector of pairs to a map in C++. For Example, Input:myVector = {{1, âoneâ}, {2, ât 2 min read Like