Open In App

Multimap of tuples in C++ with Examples

Last Updated : 27 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

What is a multimap?

In C++, a multimap is an associative container that is used to store elements in a mapped fashion. Internally, a multimap is implemented as a red-black tree. Each element of a multimap is treated as a pair. The first value is referred to as key and the second value is referred to as value. Multimap is quite similar to a map but in the case of a multimap, one can have multiple same keys. Also, one cannot use square brackets ([]) to access the value mapped with a key. Like a map, keys of the multimap are sorted in ascending order by default.

Functions associated with multimap:

  • begin(): Returns an iterator to the first element in the multimap.
  • end(): Returns an iterator to the theoretical element that follows the last element in the multimap.
  • size(): Returns the number of elements in the multimap.
  • max_size(): Returns the maximum number of elements that the multimap can hold.
  • empty(): Returns whether the multimap is empty.
  • insert(key, Value): Adds a new element or pair to the multimap.
  • erase(iterator position): Removes the element at the position pointed by the iterator.
  • erase(const x): Removes the key-value ‘x’ from the multimap.
  • clear(): Removes all the elements from the multimap.

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:

  • make_tuple(): It is used to assign tuples with values. The values passed should be in order with the values declared in the tuple.
  • 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.

How to access a Tuple?

To access elements of a tuple use the get<>() function.

Syntax:

auto fistElement = get<0>(myTuple);

auto secondElement = get<1>(myTuple);

auto thirdElement = get<2>(myTuple);

This article focuses on how to create a multimap of tuples in C++.

Multimap of tuples

A multimap of tuples is a multimap in which either of the Key or Value is a tuple on its own. Two tuples are considered to be equal if corresponding first, second, and third elements of tuples are equal. Now if there is a need to store more than one copy of a tuple along with other elements that too in a sorted or a particular order, in such cases multimap of tuples comes in handy.

Syntax:

multimap<tuples<dataType1, dataType2, dataType3>> myMultimap;

Here,

dataType1, dataType2, dataType3 can be similar or dissimilar data types.

Example 1: Below is the C++ program to demonstrate the working of a multimap of tuples.

C++




// C++ program to demonstrate
// the working of a multimap of
// tuples.
#include <bits/stdc++.h>
using namespace std;
  
// Function to print multimap elements
void print(multimap<tuple<int, int, int>, 
           bool>& myContainer)
{
    cout << "Key(tuple of integers)" << 
            "            " << 
            "Value(boolean)\n\n";
  
    for (auto currentTuple : myContainer) 
    {
        // Each element of the multiset is 
        // a pair on its own Key is of type tuple
        tuple<int, int, int> Key = currentTuple.first;
  
        // Value is a boolean value
        bool value = currentTuple.second;
  
        // Print key elements and value
        cout << '[' << get<0>(Key) << " , " << 
                get<1>(Key) << " , " << get<2>(Key) << 
                ']' << "                        " << 
                value << '\n';
    }
}
  
// Driver code
int main()
{
    // Declaring a multimap
    // Key is of tuple<int, int, int> type
    // Value is of bool type
    multimap<tuple<int, int, int>, 
             bool> myContainer;
  
    // Creating some tuples to be used
    // as keys
    tuple<int, int, int> tuple1;
    tuple1 = make_tuple(100, 200, 300);
  
    tuple<int, int, int> tuple2;
    tuple2 = make_tuple(200, 300, 400);
  
    tuple<int, int, int> tuple3;
    tuple3 = make_tuple(300, 400, 500);
  
    tuple<int, int, int> tuple4;
    tuple4 = make_tuple(100, 200, 300);
  
    // Since each element is a tuple on its 
    // own in a multimap. So, we are inserting 
    // a tuple
    // Note that [] operator doesn't working 
    // in case of a multimap
    myContainer.insert(pair<tuple<int, int, int>, 
                       bool>(tuple1, true));
    myContainer.insert(pair<tuple<int, int, int>, 
                       bool>(tuple2, false));
    myContainer.insert(pair<tuple<int, int, int>, 
                       bool>(tuple3, true));
    myContainer.insert(pair<tuple<int, int, int>, 
                       bool>(tuple4, false));
  
    // Calling print function
    print(myContainer);
  
    return 0;
}


Output

Key(tuple of integers) Value(boolean)

[100, 200, 300] 1
[100, 200, 300] 0
[200, 300, 400] 0
[300, 400, 500] 1

Example 2: Below is the C++ program to demonstrate the working of a multimap of tuples.

C++




// C++ program to demonstrate
// the working of a multimap of
// tuples.
#include <bits/stdc++.h>
using namespace std;
  
// Function to print multimap elements
void print(multimap<tuple<string, int, float>, 
           bool>& myContainer)
{
    cout << "Key(tuple of integers)" << 
            "            " << 
            "Value(boolean)\n\n";
  
    for (auto currentTuple : myContainer) 
    {
        // Each element of the multiset is 
        // a tuple on its own
        // Key is of type tuple
        tuple<string, int, float> Key = 
                           currentTuple.first;
  
        // Value is a boolean value
        bool value = currentTuple.second;
  
        // Print key elements and value
        cout << '[' << get<0>(Key) << " , " << 
                get<1>(Key) << " , " << get<2>(Key) << 
                ']' << "                        " << 
                value << '\n';
    }
}
  
// Driver code
int main()
{
    // Declaring a multimap
    // Key is of tuple<string, int, 
    // float> type
    // Value is of bool type
    multimap<tuple<string, int, float>, 
                           bool> myContainer;
  
    // Creating some tuples to be used
    // as keys
    tuple<string, int, float> tuple1;
    tuple1 = make_tuple("Java", 100, 1.123);
  
    tuple<string, int, float> tuple2;
    tuple2 = make_tuple("Geek", 200, 2.123);
  
    tuple<string, int, float> tuple3;
    tuple3 = make_tuple("HTML", 300, 3.123);
  
    tuple<string, int, float> tuple4;
    tuple4 = make_tuple("Java", 100, 1.123);
  
    // Since each element is a tuple on its 
    // own in the multimap. So, we are 
    // inserting a tuple
    // Note that [] operator doesn't working 
    // in case of a multimap
    myContainer.insert(pair<tuple<string, int, float>, 
                       bool>(tuple1, true));
    myContainer.insert(pair<tuple<string, int, float>, 
                       bool>(tuple2, false));
    myContainer.insert(pair<tuple<string, int, float>, 
                       bool>(tuple3, true));
    myContainer.insert(pair<tuple<string, int, float>, 
                       bool>(tuple4, false));
  
    // Calling print function
    print(myContainer);
  
    return 0;
}


Output

Key(tuple of integers) Value(boolean)

[Geek, 200, 2.123] 0
[HTML, 300, 3.123] 1
[Java, 100, 1.123] 1
[Java, 100, 1.123] 0



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads