 GeeksforGeeks App
Open App Browser
Continue

# 2D Vector of Tuples 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.

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 the 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 a 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.

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 a Tuple?

A tuple in C++ is an object which is used to group elements together. In a tuple, elements can be of the same data type or different data types. The elements of tuples are initialized as in the order in which they will be accessed.

Functions associated with a tuple:

1. make_tuple(): make_tuple() is used to assign tuple with values. The values passed should be in order with the values declared in the tuple.
2. get(): get() is used to access the tuple values and modify them, it accepts the index and tuple name as arguments to access a particular tuple element.

How to access a Tuple?

To access elements of a tuple use the get<>() function.

Syntax:

auto fistElement = get<0>(myTuple);
auto secondElement = get<1>(myTuple);
auto thirdElement = get<2>(myTuple);

This article focuses on how to create a 2D vector of tuples in C++.

2D Vector of Tuples

A 2D vector of tuples or vector of vectors of tuples is a vector in which each element is a vector of tuples itself. Although a tuple may contain any number of elements for simplicity, a tuple of three elements is considered.

Syntax:

vector<vector<tuple<dataType1, dataType2, dataType3>> myContainer

Here,
dataType1, dataType2, dataType3 can be similar or dissimilar data types.

Example 1: Below is the C++ program to implement 2D vector of tuples.

## C++

 `// C++ program to demonstrate the``// working of vector of vectors``// of tuples``#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` `currentTuple : myVector) {` `            ``// Print the element``            ``cout << ``"{"``;``            ``cout << get<0>(currentTuple)``                 ``<< ``", "` `<< get<1>(currentTuple)``                 ``<< ``", "` `<< get<2>(currentTuple);``            ``cout << ``"}  "``;``        ``}``        ``cout << ``"]\n"``;``    ``}``}` `// Driver code``int` `main()``{``    ``// Declaring a 2D vector of tuples``    ``vector > >``        ``myContainer;` `    ``// Initializing vectors of tuples``    ``// tuples are of type {int, int, int}``    ``vector > vect1``        ``= { { 1, 1, 2 }, { 2, 2, 4 }, { 3, 3, 6 }, { 4, 4, 8 } };` `    ``vector > vect2``        ``= { { 1, 2, 3 }, { 1, 3, 4 }, { 1, 4, 5 }, { 1, 5, 6 } };` `    ``vector > vect3``        ``= { { 4, 5, 2 }, { 8, 1, 9 }, { 9, 3, 1 }, { 2, 4, 8 } };` `    ``vector > vect4``        ``= { { 7, 2, 1 }, { 6, 5, 1 }, { 1, 2, 9 }, { 10, 4, 8 } };` `    ``// Inserting vector of tuples 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:[ {1, 1, 2} {2, 2, 4} {3, 3, 6} {4, 4, 8} ] [ {1, 2, 3} {1, 3, 4} {1, 4, 5} {1, 5, 6} ] [ {4, 5, 2} {8, 1, 9} {9, 3, 1} {2, 4, 8} ] [ {7, 2, 1} {6, 5, 1} {1, 2, 9} {10, 4, 8} ]

Example 2: Below is the C++ program to implement 2D vector of tuples.

## C++

 `// C++ program to demonstrate the``// working of vector of vectors of tuples``#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` `currentTuple : myVector) {``            ``// Print string the element``            ``cout << ``"{"``;``            ``cout << get<0>(currentTuple) << ``", "``                 ``<< get<1>(currentTuple) << ``", "``                 ``<< get<2>(currentTuple);``            ``cout << ``"}  "``;``        ``}``        ``cout << ``"]\n"``;``    ``}``}` `// Driver code``int` `main()``{``    ``// Declaring a 2D vector of tuples``    ``vector > >``        ``myContainer;` `    ``// Initializing vectors of tuples``    ``// tuples are of type {string, string, string}``    ``vector > vect1``        ``= { { ``"Geeks"``, ``"for"``, ``"Geeks"` `},``            ``{ ``"Swift"``, ``"Python"``, ``"Java"` `},``            ``{ ``"Int"``, ``"Float"``, ``"Double"` `} };` `    ``vector > vect2``        ``= { { ``"C++"``, ``"C"``, ``"C#"` `},``            ``{ ``"R"``, ``"HTML"``, ``"CSS"` `},``            ``{ ``"Javascript"``, ``"PHP"``, ``"Django"` `} };` `    ``vector > vect3``        ``= { { ``"Bhuwanesh"``, ``"Harshit"``, ``"DS"` `},``            ``{ ``"Piyush"``, ``"Jai"``, ``"Naveen"` `},``            ``{ ``"Anil"``, ``"Rahul"``, ``"keshav"` `} };` `    ``vector > vect4``        ``= { { ``"Sweta"``, ``"Tanu"``, ``"Kavita"` `},``            ``{ ``"Nawal"``, ``"Bhargav"``, ``"Jitesh"` `},``            ``{ ``"Daya"``, ``"Mohan"``, ``"Bhuwanesh"` `} };` `    ``// Inserting vector of tuples 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:[ {Geeks, for, Geeks} {Swift, Python, Java} {Int, Float, Double} ] [ {C++, C, C#} {R, HTML, CSS} {Javascript, PHP, Django} ] [ {Bhuwanesh, Harshit, DS} {Piyush, Jai, Naveen} {Anil, Rahul, keshav} ] [ {Sweta, Tanu, Kavita} {Nawal, Bhargav, Jitesh} {Daya, Mohan, Bhuwanesh} ]

Time complexity: O(n^2). // n is the total number of elements in the 2D vector.

Space complexity: O(n).

My Personal Notes arrow_drop_up