How to Create a Vector of Tuples in C++?
Last Updated :
22 Feb, 2024
In C++, a tuple is an object that allows the users to store elements of various data types together while a vector is used to store elements of the same data types. In this article, we will learn how we can create a vector of tuples in C++.
Example:
Input:
Tuple1 ={10,A,5.3} Tuple2= {20,B,6.5}Output:
Vector Elements:
Tuple 1: 10, A, 5.3
Tuple 2: 20, B, 6.5
Vector of Tuples in C++
To create a vector of tuples, we will have to first declare a vector of type tuple. It means that each element of the vector will be a tuple. Moreover, we also have to specify the type of tuple.
Syntax
vector<tuple<type1, type2, ...>> myVector.
We can simply use the std::make_tuple() method to insert the data into the vector.
For traversing, we can use the tuple::get() method to iterate over the elements of the tuple present in the vector.
C++ Program to Create a Vector of Tuples
C++
// C++ program to create a vector of tuples
#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
int main() {
// Intialize a vector of tuples
vector<tuple<int, char, float>> vecOfTuples;
// Add tuples to the vector
vecOfTuples.push_back(make_tuple(10, 'A', 3.14));
vecOfTuples.push_back(make_tuple(20, 'B', 6.28));
vecOfTuples.push_back(make_tuple(30, 'C', 9.42));
vecOfTuples.push_back(make_tuple(40, 'D', 10.8));
// Iterate over the vector and print each tuple
int i=1;
cout<<"Vector Elements:"<<endl;
for (const auto& tuple : vecOfTuples) {
// Access tuple elements using get<index>(tuple)
cout << "Tuple "<<i<<": " << get<0>(tuple) << ", " << get<1>(tuple) << ", " << get<2>(tuple) << endl;
i++;
}
return 0;
}
OutputVector Elements:
Tuple 1: 10, A, 3.14
Tuple 2: 20, B, 6.28
Tuple 3: 30, C, 9.42
Tuple 4: 40, D, 10.8
Time Complexity: O(N) where N is the number of tuples inside the vector
Auxiliary Space: O(N)
Similar Reads
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 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 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 Vector of Arrays in C++? In C++, an array is a collection of elements of a single type while vectors are dynamic arrays as they can change their size during the insertion and deletion of elements. In this article, we will learn how to create a vector of arrays in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; arr3
2 min read
How to Create a Deque of Vectors in C++? In C++, deques are sequence containers similar to queues but unlike queues, deques allow the insertion and deletion of elements from both ends efficiently. Vectors are dynamic arrays that can resize themselves during the runtime. In this article, we will learn how to create a deque of vectors in C++
2 min read