Finding all Values for a Key in Multimap using Equals_Range

Multimap is similar to the map, with several elements having identical keys. In this case, it is NOT necessary to have a single key and mapped value pair. One important thing is to note that multimap always keeps all keys in order. These multimap characteristics make it very useful for competitive programming.

Examples:

Input:

multimap<string, int> givenmap = {
{ "hello", 1 }, { "this", 2 }, { "hello", 3 },
{ "BTechGeeks", 4 }, { "hello", 5 }, { "hello", 6 },
{ "python", 7 },
};

Output:

Displaying all the values for the key hello: 
1
3
5
6

Finding all Values for a Key in Multimap using Equals_Range

1)std::multimap::equal_range

All values of a key can be found in Multimap with its equal_range() member function

It recognises the key as an argument and returns a multimap iterator pair. This returned pair has a range that shows the key entries.

Below is the implementation.

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // Initializing a multimap with key as string and value
    // as int
    std::multimap<string, int> givenmap = {
        { "hello", 1 },      { "this", 2 },  { "hello", 3 },
        { "BTechGeeks", 4 }, { "hello", 5 }, { "hello", 6 },
        { "python", 7 },
    };
    typedef multimap<string, int>::iterator mapitr;
    // It returns a pair with the range of elements with a
    // 'hello' key.
    pair<mapitr, mapitr> res
        = givenmap.equal_range("hello");
    cout << "Displaying all the values for the key hello: "
         << endl;
    // Traversing over the range
    for (mapitr itr = res.first; itr != res.second; itr++)
        cout << itr->second << endl;
    // Printing number of elements having 'hello' as key
    int hellocount = distance(res.first, res.second);
    cout << "number of elements having 'hello' as key = "
         << hellocount << endl;

    return 0;
}

Output:

Displaying all the values for the key hello: 
1
3
5
6
number of elements having 'hello' as key = 4

2)How to find whether or not a key exists on the same range multimap ()

A range of all elements with the given key returns as equal range(). So we calculate a total number of elements in a given area of iterators in order to determine whether a key does exist or not. You can do it with std:: Algorithm for distance()

If the count is 0 then the key doesn’t exist in given multimap.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // Initializing a multimap with key as string and value
    // as int
    std::multimap<string, int> givenmap = {
        { "hello", 1 },      { "this", 2 },  { "hello", 3 },
        { "BTechGeeks", 4 }, { "hello", 5 }, { "hello", 6 },
        { "python", 7 },
    };
    typedef multimap<string, int>::iterator mapitr;
    // It returns a pair with the range of elements with a
    // 'hello' key.
    pair<mapitr, mapitr> res
        = givenmap.equal_range("hello");

    // counting number of hello key  values in given
    // multimap
    int cnt = distance(res.first, res.second);
    if (cnt != 0) {
        cout
            << "number of elements having 'hello' as key = "
            << cnt << endl;
    }
    else
        cout << "Given key doesn't exit in given multimap"
             << endl;

    return 0;
}

Output:

number of elements having 'hello' as key = 4

Related Programs: