![]() |
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: 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. Syntaxvector<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 TuplesC++
Output
Vector 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 |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |