Welcome back! In this comprehensive lesson, we'll delve into the world of std::unordered_set and std::unordered_map—two powerful data structures introduced in C++11. These data structures will help us manage data efficiently, making them essential for real-world programming projects. Let's get started!
std::unordered_set is a data structure that implements an unordered associative container—a collection of unique elements that can be accessed using an iterator or by their keys. The key difference between std::unordered_set and a regular set is that the elements in an unordered_set are stored in a hash table, which makes it faster in terms of search and insert operations.
To create an std::unordered_set, we use the following syntax:
#include <unordered_set>
std::unordered_set<KeyType> unorderedSetName;Replace KeyType with the type of elements you want to store in the set. For example, to create a set that stores integers:
std::unordered_set<int> myUnorderedSet;To add elements to an std::unordered_set, we use the insert() function:
myUnorderedSet.insert(element);Here's an example:
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> myUnorderedSet;
myUnorderedSet.insert(1);
myUnorderedSet.insert(2);
myUnorderedSet.insert(3);
myUnorderedSet.insert(1); // Since 1 already exists, it won't be added
// Printing the set
for (auto it = myUnorderedSet.begin(); it != myUnorderedSet.end(); ++it) {
std::cout << *it << " ";
}
return 0;
}Output:
1 2 3
To access elements in an std::unordered_set, we use an iterator:
for (auto it = myUnorderedSet.begin(); it != myUnorderedSet.end(); ++it) {
std::cout << *it << " ";
}To check if an element exists in an std::unordered_set, we can use the find() function:
if (myUnorderedSet.find(element) != myUnorderedSet.end()) {
std::cout << "Element found" << std::endl;
} else {
std::cout << "Element not found" << std::endl;
}To remove an element from an std::unordered_set, we use the erase() function:
myUnorderedSet.erase(element);std::unordered_map is an unordered associative container that stores unique key-value pairs. It uses a hash table for faster search and insert operations.
To create an std::unordered_map, we use the following syntax:
#include <unordered_map>
std::unordered_map<KeyType, ValueType> unorderedMapName;Replace KeyType and ValueType with the types of keys and values you want to store in the map. For example, to create a map that stores integers as keys and strings as values:
std::unordered_map<int, std::string> myUnorderedMap;To add elements to an std::unordered_map, we use the insert() function:
myUnorderedMap.insert({key, value});Here's an example:
#include <iostream>
#include <unordered_map>
int main() {
std::unordered_map<int, std::string> myUnorderedMap;
myUnorderedMap.insert({1, "One"});
myUnorderedMap.insert({2, "Two"});
myUnorderedMap.insert({3, "Three"});
// Printing the map
for (auto it = myUnorderedMap.begin(); it != myUnorderedMap.end(); ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
return 0;
}Output:
1: One
2: Two
3: Three
To access elements in an std::unordered_map, we use an iterator or the at() function (which returns an error if the key is not found):
std::cout << myUnorderedMap.at(1); // Outputs: OneTo check if a key exists in an std::unordered_map, we can use the find() function:
if (myUnorderedMap.find(key) != myUnorderedMap.end()) {
std::cout << "Key found" << std::endl;
} else {
std::cout << "Key not found" << std::endl;
}To remove an element from an std::unordered_map, we use the erase() function:
myUnorderedMap.erase(key);By learning std::unordered_set and std::unordered_map, you've gained essential skills for managing data efficiently in C++11. These data structures will come in handy for real-world programming projects and make your code faster and more practical. Happy coding! 🚀