Map of Tuples in C++ with Examples

19 Dec, 2021
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.
Syntax:
map<tuple<dataType1, dataType2, dataType3>, dataType>;
Here, 
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.
C++
// CPP program to demonstrate // the working of a map of // tuples.#include <bits/stdc++.h>using namespace std;  // Function to print map elementsvoid print(map<tuple<int, int,            int>, int> &mapOfTuple){  cout << "   Key(Tuple)            " <<           "Value(Sum)\n\n";  for (auto pr : mapOfTuple)      // pr points to current pair of mapOfTuple    cout << "[" << get<0>(pr.first) << ", " <<             get<1>(pr.first) << ", " <<             get<2>(pr.first) << "]            " <<             pr.second << "\n";    }  // Driver codeint main(){  // Sending the hash function  // as a third argument  map<tuple<int, int, int>,             int> mapOfTuple;    // Creating some tuples to be used   // as keys  tuple<int, int,         int> tuple1(100, 200, 300);  tuple<int, int,         int> tuple2(400, 500, 600);  tuple<int, int,         int> tuple3(700, 800, 900);    // Mapping sum of tuple elements as values  mapOfTuple[tuple1] = get<0>(tuple1) +                        get<1>(tuple1) +                        get<2>(tuple1);  mapOfTuple[tuple2] = get<0>(tuple2) +                        get<1>(tuple2) +                        get<2>(tuple2);  mapOfTuple[tuple3] = get<0>(tuple3) +                        get<1>(tuple3) +                        get<2>(tuple3);    // Calling print function  print(mapOfTuple);    return 0;}
Output:
Key(Tuple)            Value(Sum)
[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.
C++
// C++ program to demonstrate // the working of a map of // tuples.#include <bits/stdc++.h>using namespace std;  // Function to print map elementsvoid print(map<tuple<string, string,            string>, string> &mapOfTuple){  cout << "   Key(Tuple)             " <<          "Value(Concatenation)\n\n";    // Iterating over map using range-based loop  for (auto pr : mapOfTuple)      // pr points to current pair of mapOfTuple    cout << "[" << get<0>(pr.first) <<             ", " << get<1>(pr.first) <<             ", " << get<2>(pr.first) <<             "]         " << pr.second << "\n";}  // Driver codeint main(){  // Declaring a map whose key is a   // tuple of strings value is of   // also string type   map<tuple<string, string, string>,             string> mapOfTuple;    // Creating some tuples of string types   // to be used as keys  tuple<string, string, string>   tuple1("Geeks", "for", "Geeks");  tuple<string, string, string>   tuple2("R", "HTML", "Javascript");  tuple<string, string, string>   tuple3("Python", "Swift", "Java");    // Mapping concatenation of tuple elements as values  mapOfTuple[tuple1] = get<0>(tuple1) + " " +                        get<1>(tuple1) + " " +                        get<2>(tuple1);  mapOfTuple[tuple2] = get<0>(tuple2) + " " +                        get<1>(tuple2) + " " +                        get<2>(tuple2);  mapOfTuple[tuple3] = get<0>(tuple3) + " " +                        get<1>(tuple3) + " " +                        get<2>(tuple3);    // Calling print function  print(mapOfTuple);    return 0;}
Output:
 Key(Tuple)             Value(Concatenation)
[Geeks, for, Geeks]         Geeks for Geeks
[Python, Swift, Java]         Python Swift Java
[R, HTML, Javascript]         R HTML Javascript
cpp-map
cpp-tuple
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.