C++11 std::unordered_set/map: A Powerful Tool for Efficient Data Management 🎯

beginner
9 min

C++11 std::unordered_set/map: A Powerful Tool for Efficient Data Management 🎯

Introduction 📝

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 📝

What is std::unordered_set?

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.

Creating an Unordered Set 📝

To create an std::unordered_set, we use the following syntax:

cpp
#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:

cpp
std::unordered_set<int> myUnorderedSet;

Inserting Elements 📝

To add elements to an std::unordered_set, we use the insert() function:

cpp
myUnorderedSet.insert(element);

Here's an example:

cpp
#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

Accessing Elements 📝

To access elements in an std::unordered_set, we use an iterator:

cpp
for (auto it = myUnorderedSet.begin(); it != myUnorderedSet.end(); ++it) { std::cout << *it << " "; }

Checking Presence of Elements 📝

To check if an element exists in an std::unordered_set, we can use the find() function:

cpp
if (myUnorderedSet.find(element) != myUnorderedSet.end()) { std::cout << "Element found" << std::endl; } else { std::cout << "Element not found" << std::endl; }

Erasing Elements 📝

To remove an element from an std::unordered_set, we use the erase() function:

cpp
myUnorderedSet.erase(element);

std::unordered_map 📝

What is std::unordered_map?

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.

Creating an Unordered Map 📝

To create an std::unordered_map, we use the following syntax:

cpp
#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:

cpp
std::unordered_map<int, std::string> myUnorderedMap;

Inserting Elements 📝

To add elements to an std::unordered_map, we use the insert() function:

cpp
myUnorderedMap.insert({key, value});

Here's an example:

cpp
#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

Accessing Elements 📝

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):

cpp
std::cout << myUnorderedMap.at(1); // Outputs: One

Checking Presence of Keys 📝

To check if a key exists in an std::unordered_map, we can use the find() function:

cpp
if (myUnorderedMap.find(key) != myUnorderedMap.end()) { std::cout << "Key found" << std::endl; } else { std::cout << "Key not found" << std::endl; }

Erasing Elements 📝

To remove an element from an std::unordered_map, we use the erase() function:

cpp
myUnorderedMap.erase(key);

Conclusion 🎯

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! 🚀