Standard Template Library: Using the unordered_multimap
What is it?
C++ STL unordered_multimap
is an associative container that stores elements in an unordered manner. It is similar to std::multimap
, except that their keys do not order the elements. Instead, they are stored in an unordered manner using a hash table.
unordered_multimap
is implemented using a hash table, which provides fast access to elements based on their keys. It allows for constant-time average case access to elements, making it a good choice for large datasets.
Syntax
The syntax for declaring an unordered_multimap
is:
std::unordered_multimap<key_type, value_type> myMap;
Where,
key_type
: The data type of the key used to index the elements in the map.value_type
: The data type of the values stored in the map.
How to use it
To use unordered_multimap
, follow the below steps:
- Create an
unordered_multimap
container. - Insert elements into the map using the
insert()
method. - Access elements in the map using the
at()
method or thefind()
method. - Remove elements from the map using the
erase()
method.
Program code snippet example
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
int main() {
// Creating an unordered_multimap container.
unordered_multimap<string, int> myMap;
// Inserting elements into the map.
myMap.insert(make_pair("John", 30));
myMap.insert(make_pair("Mary", 40));
myMap.insert(make_pair("Bob", 50));
myMap.insert(make_pair("Tom", 60));
myMap.insert(make_pair("John", 70));
// Accessing elements in the map.
cout << "Value at key 'John': ";
cout << myMap.find("John")->second << endl;
// Removing an element from the map.
myMap.erase("Mary");
// Accessing elements in the map using the 'at()' method.
cout << "Value at key 'Bob': ";
cout << myMap.at("Bob") << endl;
return 0;
}
Output:
Value at key 'John': 30
Value at key 'Bob': 50
Output elements of myMap
using a loop.
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
int main() {
// Creating an unordered_multimap container.
unordered_multimap<string, int> myMap;
// Inserting elements into the map.
myMap.insert(make_pair("John", 30));
myMap.insert(make_pair("Mary", 40));
myMap.insert(make_pair("Bob", 50));
myMap.insert(make_pair("Tom", 60));
myMap.insert(make_pair("John", 70));
// Accessing elements in the map using a loop.
for (auto& elem : myMap) {
cout << "Key: " << elem.first << ", Value: " << elem.second << endl;
}
return 0;
}
Output:
Key: Tom, Value: 60
Key: Bob, Value: 50
Key: John, Value: 30
Key: John, Value: 70
Key: Mary, Value: 40
Instruction: You are given a list of students and their grades. Output the names of students who have a grade of 90 or higher. If multiple students have the same name, output each name only once. The input will contain one line for each student, which contains the student's first name, last name, and grade separated by spaces. The input ends with a line containing only the word end
.
Example Input:
John Doe 85
Jane Smith 90
Bob Johnson 80
Jane Smith 95
end
Example Output:
Jane Smith