Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New Entry] Unordered Maps in C++ #5347

Merged
merged 7 commits into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions content/cpp/concepts/unordered-map/unordered-map.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
---
Title: 'Unordered Maps'
Description: 'Unordered Maps are associative containers with elements with key-value pairs.'
Subjects:
- 'Computer Science'
- 'Game Development'
Tags:
- 'Objects'
- 'OOP'
- 'Classes'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

**`Unordered Maps`** are associative containers that have elements with key-value pairs. Unlike Maps, the pairs in Unordered Maps are not sorted by their keys. Each mapped value must have a unique key value.

## Syntax

An empty unordered map can be created by using the `unordered_map` keyword, declaring the data types of the key and value, and setting a `mapName`:

```pseudo
std::unordered_map<type1, type2> mapName;
```

- `type1`: Date type of the key in the unordered_map.
- `type2`: Date type of the value in the unordered_map.

> **Note:** To use unordered_map, including the `unordered_map` library is necessary.

## Examples

The below example shows how to use `unordered_map`.

```cpp
#include <iostream>
#include <unordered_map>

int main() {
std::unordered_map<std::string, int> myMap;

// Inserting elements
myMap["apple"] = 2;
myMap["banana"] = 5;
myMap["orange"] = 3;

// Iterating over elements
for (const std::pair<const std::string, int>& x : myMap) {
std::cout << x.first << " " << x.second << std::endl;
}
return 0;
}
```

The output will be:

```shell
orange 3
banana 5
apple 2
```

### Overriding Functions

By default, keys are treated as `case-sensitive`, but this can be avoided, so they can be treated as `case-insensitive`. In the example below, the keys are treated as `case-insensitive`.

```cpp
#include <iostream>
#include <unordered_map>
#include <string>

struct CustomHash {
std::size_t operator()(const std::string & key) const {
// Custom hash: hash based on the length of the string
return key.length();
}
};

struct CustomEqual {
bool operator()(const std::string & lhs,
const std::string & rhs) const {
// Custom equality: compare strings ignoring case
return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(),
[](char a, char b) {
return tolower(a) == tolower(b);
});
}
};

int main() {
// Unordered map with custom hash and equality functions
std::unordered_map < std::string, int, CustomHash, CustomEqual > myMap;

myMap["apple"] = 1;

// Will be treated as the same key as "apple"
myMap["APPLE"] = 2;

for (const auto & pair: myMap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}
```

The above code snippet will return the following output:

```shell
apple: 2
```

## Codebyte Example

```codebyte/cpp
#include <iostream>
#include <unordered_map>

int main() {
// Create an unordered_map with default hash and equality functions
std::unordered_map<int, std::string> myMap;

// Insert elements into the unordered_map
myMap[1] = "one";
myMap[2] = "two";
myMap[3] = "three";

// Print the elements in the unordered_map
std::cout << "Elements in the unordered_map:" << std::endl;
for (const auto& pair : myMap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}
```
Loading