C++ Unordered_multimap::equal_range() Function



The C++ std::unordered_multimap::equal_range() function is used to return the pair of iterators that represent the range of elements that have keys equivalent to a specified key.

As we know equal_range() function returns a pair of iterators, where the first iterator points to the first element in the range that is equivalent to the specified key, and the second iterator points past the last element in the range. if no element in the unordered_multimap is equivalent to the specified key, both iterators (lower and upper) in the returned pair will be equal to to the position past the end of the container or unordered_multimap.end().

Syntax

Following is the syntax of std::unordered_multimap::equal() function.

std::pair<iterator, iterator> equal_range(const key_type& key);

Parameters

  • k − It indicates the key value to be compared or searched.

Return value

This function returns a pair of iterators.

Example 1

In the following example, we are demonstrating the usages of the unordered_multimap::equal_range() function.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<char, int> um = {
      {'a', 1},
      {'b', 2},
      {'c', 3},
      {'e', 4},
      {'a', 5},
      {'d', 6},
      {'e', 5}
   };
   auto ret = um.equal_range('e');
   cout << "Lower bound is " << ret.first->first 
       << " = "<< ret.first->second << endl;
   cout << "Upper bound is " << ret.second->first
       << " = " << ret.second->second << endl;
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

Lower bound is e = 5
Upper bound is c = 3

Example 2

Consider the following example, where we are going to access the values associated with the keys in the range.

#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
   unordered_multimap<int, string> um = {
      {1, "one"},
      {2, "two"},
      {4, "four"},
      {3, "three"},
      {4, "four"},
      {5, "five"},
      {3, "three"},
   };
   auto range = um.equal_range(3);

   // Check if the key was found
   if (range.first != um.end()) {
      cout << "Key 3 found in the multimap!" << endl;
      // Access the values associated with keys in the range
      for (auto it = range.first; it != range.second; ++it) {
          cout << "The value associated with key " << it->first << " is: " << it->second << endl;
      }
   } else {
      cout << "Key 3 not found in the multimap!" << endl;
   }
   return 0;
}

Output

If we run the above code it will generate the following output −

Key 3 found in the multimap!
The value associated with key 3 is: three
The value associated with key 3 is: third

Example 3

Let's look at the following example, where we are going to get both the value of the duplicate key and also the upper and lower bounds of the specified key.

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
typedef unordered_multimap<string,string> stringmultimap;
int main () {
   stringmultimap umm = {
      {"orange","FL"},
      {"strawberry","LA"},
      {"strawberry","OK"},
      {"pumpkin","NH"}
   };
   cout << "Entries with strawberry: "<<endl;
   auto range = umm.equal_range("strawberry");
   for (auto it = range.first; it != range.second; ++it){
      cout<< it->first << " is: " << it->second<<endl;
   }
   cout << "Lower bound is " << range.first->first
      << " = "<< range.first->second << endl;
   cout << "Upper bound is " << range.second->first
      << " = " << range.second->second << endl;
   return 0;
}

Output

Following is the output of the above code −

Entries with strawberry: 
strawberry is: OK
strawberry is: LA
Lower bound is strawberry = OK
Upper bound is pumpkin = NH

Example 4

Following is the example, where we are going to use the multimap and iterate over pairs pointing to the range.

#include <iostream>
#include <unordered_map>
using namespace std;

int main() {
   unordered_multimap<int, int> umm;    
   umm.insert({ 1, 2 });
   umm.insert({ 1, 2 });
   umm.insert({ 2, 3 });
   umm.insert({ 3, 4 });
   umm.insert({ 2, 6 });
   auto range = umm.equal_range(1);
   cout << "Elements with Key 1: ";
   for (auto it = range.first; it != range.second; it++) {
      cout << it->second << " ";
   }
   
   cout << endl;
   range = umm.equal_range(2);
   cout << "Elements with Key 2: ";
   for (auto it = range.first; it != range.second; it++) {
      cout << it->second << " ";
   }
   return 0;
}

Output

Output of the above code is as follows −

Elements with Key 1: 2 2 
Elements with Key 2: 6 3 
Advertisements