Map of Tuples in C++ with Examples
What is a tuple?
A tuple in C++ is an object that has the ability to group a number of elements. The elements can be of the same type as well as different data types. The order in which tuple elements are initialized can be accessed in the same order.
Functions associated with a tuple:
1. make_tuple(): It is used to assign tuples with values. The values passed should be in order with the values declared in the tuple.
2. get(): It 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.
What is map?
Maps in C++ are associative containers that can store elements in a mapped fashion. Each element of a map has a key and the corresponding mapped value. No two mapped values can have the same key values. A map follows the below syntax,
Functions associated with Map:
- begin(): Returns an iterator to the first element in the map
- end(): Returns an iterator to the hypothetical element that follows the last element in the map
- size(): Returns the number of elements in the map
- max_size(): Returns the maximum number of elements that the map can hold
- empty(): Returns whether the map is empty
- clear(): Removes all the elements from the map
Map of Tuples
A map of tuples is a map in which either of the key or values is a tuple.
map<tuple<dataType1, dataType2, dataType3>, dataType>;
dataType1, dataType2, dataType3 are the data types for the tuple which is a key here
dataType is the data type for value
This article focuses on how to create a map of tuples in C++. Although one can make a tuple of more or fewer elements also but for simplicity, In this article, we have used tuples having only three elements.
Example 1: Below is the C++ program to demonstrate the working of a map of tuples.
[100, 200, 300] 600
[400, 500, 600] 1500
[700, 800, 900] 2400
Example 2: Below is the C++ program to demonstrate the working of a map of tuples.
[Geeks, for, Geeks] Geeks for Geeks
[Python, Swift, Java] Python Swift Java