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++
// CPP program to demonstrate
// the working of a map of
// tuples.
#include <bits/stdc++.h>
using namespace std;
// Function to print map elements
void 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 code
int 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 elements
void 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 code
int 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
Similar Reads
Set of Tuples in C++ with Examples
What is a tuple?A tuple is an object that can hold a number of elements. The elements can be of different data types. The elements of tuples are initialized as arguments in the order in which they will be accessed. Operations on tuple:1. get(): get() is used to access the tuple values and modify the
4 min read
Multiset of Tuples in C++ with Examples
What is a tuple? A tuple in C++ is an object which binds a group of elements together. The elements can be similar as well as different data types. The elements of tuples are initialized as in the order in which they will be accessed. Syntax: tuple<data_type1, data_type2, dataType3, ....> myTu
5 min read
Map of Vectors in C++ STL with Examples
Map in STL Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. Vector in STL Vector is same as dynamic arrays with the ability to resize itself automatically when an element is insert
2 min read
Deque of Tuples in C++ with Examples
What is deque? In C++, a deque is a sequence container and it is also known by the name, double-ended queue. As the name implies, a deque allows insertion and deletion from both ends. Although a deque is similar to a vector, deques are more efficient compared to vectors. In vectors, contiguous stora
8 min read
Map of Sets in C++ STL with Examples
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The
2 min read
2D Vector of Tuples in C++ with Examples
What is Vector? In C++, a vector is similar to dynamic arrays with the ability to resize itself automatically. Vector elements are stored in contiguous memory locations so that they can be accessed and traversed using iterators. Functions associated with a vector: begin(): Returns an iterator pointi
6 min read
Priority Queue of Maps in C++ with Examples
Priority Queue Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements are in non-increasing order (hence we can see that each element of the queue has a priority {fixed order}). Genera
15+ min read
Multimap vs Map in C++ STL with Examples
Map in C++ STL Map stores unique key-value pairs in a sorted manner. Each key is uniquely associated with a value that may or may not be unique. A key can be inserted or deleted from a map but cannot be modified. Values assigned to keys can be changed. It is a great way for quickly accessing value u
8 min read
unordered set of tuples in C++ with Examples
What is a tuple? A tuple in C++ is an object which is used to group elements together. In a tuple, elements can be of the same data type or different data types. The elements of tuples are initialized as in the order in which they will be accessed. Functions associated with a tuple: 1. make_tuple():
6 min read
Multiset of Pairs in C++ with Examples
What is Multiset? A multiset is an associative container that can hold a number of elements in a specific order. Unlike a set, a multiset can contain multiple occurrences of the same element. Some of the functions associated with a multiset: begin(): Returns an iterator to the first element in the m
4 min read