|
| 1 | +--- |
| 2 | +Title: '.reserve()' |
| 3 | +Description: 'Reserves space for at least the specified number of elements in an unordered map.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Game Development' |
| 7 | +Tags: |
| 8 | + - 'Classes' |
| 9 | + - 'Map' |
| 10 | + - 'Memory' |
| 11 | + - 'Objects' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-c-plus-plus' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +In C++, the **`.reserve()`** method reserves space for at least the specified number of elements in an unordered map, helping optimize performance by reducing costly rehash operations during bulk insertions. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +mapName.reserve(count); |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `count`: The minimum number of elements to reserve space for. |
| 28 | + |
| 29 | +**Return value:** |
| 30 | + |
| 31 | +This is a `void` method. It doesn’t return a value but affects the internal capacity of the unordered map. |
| 32 | + |
| 33 | +## Example |
| 34 | + |
| 35 | +This example demonstrates the use of the `.reserve()` method with an unordered map to optimize performance when adding student grades: |
| 36 | + |
| 37 | +```cpp |
| 38 | +#include <iostream> |
| 39 | +#include <unordered_map> |
| 40 | + |
| 41 | +int main() { |
| 42 | + // Initializing unordered_map and reserving space for 10 elements |
| 43 | + std::unordered_map<std::string, int> studentGrades; |
| 44 | + studentGrades.reserve(10); |
| 45 | + |
| 46 | + // Adding student grades |
| 47 | + studentGrades["Alice"] = 95; |
| 48 | + studentGrades["Bob"] = 87; |
| 49 | + studentGrades["Charlie"] = 92; |
| 50 | + studentGrades["Diana"] = 88; |
| 51 | + |
| 52 | + std::cout << "Student grades after reservation:\n"; |
| 53 | + for (const auto& student : studentGrades) { |
| 54 | + std::cout << student.first << ": " << student.second << std::endl; |
| 55 | + } |
| 56 | + |
| 57 | + std::cout << "Bucket count: " << studentGrades.bucket_count() << std::endl; |
| 58 | + |
| 59 | + return 0; |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +Here is the output: |
| 64 | + |
| 65 | +```shell |
| 66 | +Student grades after reservation: |
| 67 | +Charlie: 92 |
| 68 | +Bob: 87 |
| 69 | +Diana: 88 |
| 70 | +Alice: 95 |
| 71 | +Bucket count: 11 |
| 72 | +``` |
| 73 | + |
| 74 | +## Codebyte Example |
| 75 | + |
| 76 | +This codebyte example demonstrates the use of the `.reserve()` method with an unordered map to store employee information efficiently: |
| 77 | + |
| 78 | +```codebyte/cpp |
| 79 | +#include <iostream> |
| 80 | +#include <unordered_map> |
| 81 | +
|
| 82 | +int main() { |
| 83 | + // Initializing unordered_map with employee IDs and names |
| 84 | + std::unordered_map<int, std::string> employees; |
| 85 | +
|
| 86 | + // Reserve space for 8 employees to optimize performance |
| 87 | + employees.reserve(8); |
| 88 | +
|
| 89 | + // Adding employee data |
| 90 | + employees[101] = "John"; |
| 91 | + employees[102] = "Sarah"; |
| 92 | + employees[103] = "Mike"; |
| 93 | + employees[104] = "Emma"; |
| 94 | +
|
| 95 | + std::cout << "Employee roster:\n"; |
| 96 | + for (const auto& emp : employees) { |
| 97 | + std::cout << "ID " << emp.first << ": " << emp.second << std::endl; |
| 98 | + } |
| 99 | +
|
| 100 | + std::cout << "Reserved bucket count: " << employees.bucket_count() << std::endl; |
| 101 | +
|
| 102 | + return 0; |
| 103 | +} |
| 104 | +``` |
0 commit comments