- C++ Basics
- C++ Introduction
- C++ Installation
- C++ Syntax
- C++ Hello World
- C++ Comments
- C++ Variables
- C++ Data Types
- C++ Constants
- C++ Type Casting
- C++ Inline
- C++ File Inclusion
- C++ Date & Time
- C++ Return Types
- C++ Object Oriented
- C++ Classes
- C++ Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Exceptions
- C++ Advanced
- C++ Conditions
- C++ Loops
- C++ Functions
- C++ Structures
- C++ Enums
- C++ References
- C++ Pointers
- C++ Data Structures
- C++ Libs
- C++ Data Structures
- C++ Arrays
- C++ Vectors
- C++ Lists
- C++ Linked List
- C++ Deque
- C++ Stacks
- C++ Queues
- C++ Priority Queues
- C++ Sets
- C++ Maps
- C++ Unordered Sets and Maps
- C++ Graphs
C++ Unordered Sets and Maps
In C++, std::unordered_set and std::unordered_map are associative containers provided by the Standard Template Library (STL) that store elements based on hash tables. These containers allow for average constant-time complexity for insertions, deletions, and lookups, making them suitable for scenarios where fast access is required and ordering is not important.
std::unordered_set
std::unordered_set is similar to std::set but uses a hash table for storage. Elements are stored based on their hash values rather than a strict order.
Basic Operations with std::unordered_set
Include the Necessary Header:
cpp#include#include Declare an Unordered Set:
cppstd::unordered_set<int> mySet;Add Elements:
cppmySet.insert(5); mySet.insert(10); mySet.insert(3);Check for Presence of an Element:
cppif (mySet.find(10) != mySet.end()) { std::cout << "10 is in the unordered set." << std::endl; }Remove Elements:
cppmySet.erase(5); // Removes the element with value 5Iterate Through the Unordered Set:
cppfor (const auto& elem : mySet) { std::cout << elem << " "; } std::cout << std::endl;Get the Size of the Unordered Set:
cppstd::cout << "Size of the unordered set: " << mySet.size() << std::endl;Check if the Unordered Set is Empty:
cppif (mySet.empty()) { std::cout << "Unordered set is empty." << std::endl; }
std::unordered_map
std::unordered_map is similar to std::map but uses a hash table for storage. It stores key-value pairs, with keys being unique.
Basic Operations with std::unordered_map
Include the Necessary Header:
cpp#include#include Declare an Unordered Map:
cppstd::unordered_map<int, std::string> myMap;Add Elements:
cppmyMap[1] = "one"; myMap[2] = "two"; myMap[3] = "three";Access Elements:
cppstd::cout << "Value associated with key 2: " << myMap[2] << std::endl;You can also use the
atmethod:cpptry { std::cout << "Value associated with key 4: " << myMap.at(4) << std::endl; } catch (const std::out_of_range& e) { std::cout << "Key 4 does not exist." << std::endl; }Check for Presence of a Key:
cppif (myMap.find(3) != myMap.end()) { std::cout << "Key 3 exists in the unordered map." << std::endl; }Remove Elements:
cppmyMap.erase(2); // Removes the element with key 2Iterate Through the Unordered Map:
cppfor (const auto& pair : myMap) { std::cout << pair.first << ": " << pair.second << std::endl; }Get the Size of the Unordered Map:
cppstd::cout << "Size of the unordered map: " << myMap.size() << std::endl;Check if the Unordered Map is Empty:
cppif (myMap.empty()) { std::cout << "Unordered map is empty." << std::endl; }
Custom Hash Functions
You can define custom hash functions and equality predicates if you need special handling for the keys in your unordered containers.
Define a Custom Hash Function:
cppstruct CustomHash { std::size_t operator()(int key) const { return std::hash<int>{}(key) ^ 0x12345678; // Example custom hash function } };Define a Custom Equality Predicate:
cppstruct CustomEqual { bool operator()(int lhs, int rhs) const { return lhs % 10 == rhs % 10; // Example custom equality function } };Use Custom Hash Function and Equality Predicate:
cppstd::unordered_set<int, CustomHash, CustomEqual> customSet;
Example Code
Here’s a complete example demonstrating basic operations for both std::unordered_set and std::unordered_map:
cpp#include
#include
#include
#include
// Custom hash function
struct CustomHash {
std::size_t operator()(int key) const {
return std::hash<int>{}(key) ^ 0x12345678;
}
};
// Custom equality predicate
struct CustomEqual {
bool operator()(int lhs, int rhs) const {
return lhs % 10 == rhs % 10;
}
};
int main() {
// Unordered set
std::unordered_set<int> mySet = {5, 10, 3, 7};
std::cout << "Unordered set:" << std::endl;
for (const auto& elem : mySet) {
std::cout << elem << " ";
}
std::cout << std::endl;
// Unordered map
std::unordered_map<int, std::string> myMap = {{1, "one"}, {2, "two"}, {3, "three"}};
std::cout << "Unordered map:" << std::endl;
for (const auto& pair : myMap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
// Custom unordered set
std::unordered_set<int, CustomHash, CustomEqual> customSet = {5, 15, 25}; // Example with custom hash and equality
std::cout << "Custom unordered set:" << std::endl;
for (const auto& elem : customSet) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Output
yamlUnordered set:
5 10 3 7
Unordered map:
1: one
2: two
3: three
Custom unordered set:
5 15 25
This example demonstrates basic usage and custom configuration of unordered associative containers.