Map of Tuples in C++ with Examples
Last Updated :
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++
#include <bits/stdc++.h>
using namespace std;
void print(map<tuple< int , int ,
int >, int > &mapOfTuple)
{
cout << " Key(Tuple) " <<
"Value(Sum)\n\n" ;
for ( auto pr : mapOfTuple)
cout << "[" << get<0>(pr.first) << ", " <<
get<1>(pr.first) << ", " <<
get<2>(pr.first) << "] " <<
pr.second << "\n" ;
}
int main()
{
map<tuple< int , int , int >,
int > mapOfTuple;
tuple< int , int ,
int > tuple1(100, 200, 300);
tuple< int , int ,
int > tuple2(400, 500, 600);
tuple< int , int ,
int > tuple3(700, 800, 900);
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);
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++
#include <bits/stdc++.h>
using namespace std;
void print(map<tuple<string, string,
string>, string> &mapOfTuple)
{
cout << " Key(Tuple) " <<
"Value(Concatenation)\n\n" ;
for ( auto pr : mapOfTuple)
cout << "[" << get<0>(pr.first) <<
", " << get<1>(pr.first) <<
", " << get<2>(pr.first) <<
"] " << pr.second << "\n" ;
}
int main()
{
map<tuple<string, string, string>,
string> mapOfTuple;
tuple<string, string, string>
tuple1( "Geeks" , "for" , "Geeks" );
tuple<string, string, string>
tuple2( "R" , "HTML" , "Javascript" );
tuple<string, string, string>
tuple3( "Python" , "Swift" , "Java" );
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);
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...