![]() |
VOOZH | about |
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:
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:
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.
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++.
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.
Example 2: Below is the C++ program to implement 2D vector of tuples.
Time complexity: O(n^2). // n is the total number of elements in the 2D vector.
Space complexity: O(n).