Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

2D Vector of Tuples in C++ with Examples

  • Last Updated : 27 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.

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<>()

ction.

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 <bits/stdc++.h>
using namespace std;
  
// Function to print 2D vector elements
void print(vector<vector<tuple<int,
                               int, int> > >& myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) {
        // Each element of the 2D vector
        // is a vector itself
        vector<tuple<int, int, int> > myVector
            = currentVector;
  
        // Iterating over the 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<vector<tuple<int,
                        int, int> > >
        myContainer;
  
    // Initializing vectors of tuples
    // tuples are of type {int, int, int}
    vector<tuple<int, int, int> > vect1
        = { { 1, 1, 2 }, { 2, 2, 4 }, { 3, 3, 6 }, { 4, 4, 8 } };
  
    vector<tuple<int, int, int> > vect2
        = { { 1, 2, 3 }, { 1, 3, 4 }, { 1, 4, 5 }, { 1, 5, 6 } };
  
    vector<tuple<int, int, int> > vect3
        = { { 4, 5, 2 }, { 8, 1, 9 }, { 9, 3, 1 }, { 2, 4, 8 } };
  
    vector<tuple<int, int, int> > 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 <bits/stdc++.h>
using namespace std;
  
// Function to print 2D vector elements
void print(vector<vector<tuple<string,
                               string, string> > >& myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer) {
        // Each element of the 2D vector
        // is a vector itself
        vector<tuple<string, string, string> > myVector
            = currentVector;
  
        // Iterating over the the vector
        // elements
        cout << "[  ";
        for (auto currentTuple : myVector) {
            // Prstring 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<vector<tuple<string,
                        string, string> > >
        myContainer;
  
    // Initializing vectors of tuples
    // tuples are of type {string, string, string}
    vector<tuple<string, string, string> > vect1
        = { { "Geeks", "for", "Geeks" },
            { "Swift", "Python", "Java" },
            { "Int", "Float", "Double" } };
  
    vector<tuple<string, string, string> > vect2
        = { { "C++", "C", "C#" },
            { "R", "HTML", "CSS" },
            { "Javascript", "PHP", "Django" } };
  
    vector<tuple<string, string, string> > vect3
        = { { "Bhuwanesh", "Harshit", "DS" },
            { "Piyush", "Jai", "Naveen" },
            { "Anil", "Rahul", "keshav" } };
  
    vector<tuple<string, string, string> > 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} ]


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!