|
| 1 | +--- |
| 2 | +Title: '.end()' |
| 3 | +Description: 'Returns an iterator referring to the past-the-end element in a map container.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Data Structures' |
| 9 | + - 'Iterators' |
| 10 | + - 'Map' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-c-plus-plus' |
| 13 | + - 'paths/computer-science' |
| 14 | +--- |
| 15 | + |
| 16 | +The **`.end()`** method is a [built-in function](https://www.codecademy.com/resources/docs/cpp/functions) in C++ Standard Template Library(STL), which is defined in the `<map>` header file. It returns an iterator pointing to the theoretical element just after the last element in the [map](https://www.codecademy.com/resources/docs/cpp/maps) container. This element is referred to as the "past-the-end" element and does not represent an actual element in the map container. It serves as a marker indicating the end of the map sequence and should not be dereferenced. |
| 17 | + |
| 18 | +The `.end()` method is commonly used in combination with `.begin()` to specify a range for iterating through all elements in a map container. This iterator-based approach is a fundamental pattern in the C++ Standard Template Library (STL) and is used across various algorithms and container operations. |
| 19 | + |
| 20 | +## Syntax |
| 21 | + |
| 22 | +```pseudo |
| 23 | +map_name.end(); |
| 24 | +``` |
| 25 | + |
| 26 | +**Parameters:** |
| 27 | + |
| 28 | +The `.end()` method does not accept any parameters. |
| 29 | + |
| 30 | +**Return value:** |
| 31 | + |
| 32 | +It returns an iterator pointing to the past-the-end element in the map container. If the map object is const-qualified, the function returns a `const_iterator`. Otherwise, it returns an `iterator`. |
| 33 | + |
| 34 | +## Example 1: Basic Map Iteration Using `.end()` |
| 35 | + |
| 36 | +This example demonstrates how to use `.end()` with `.begin()` to iterate through all key-value pairs in a map: |
| 37 | + |
| 38 | +```cpp |
| 39 | +#include <iostream> |
| 40 | +#include <map> |
| 41 | + |
| 42 | +int main() { |
| 43 | + // Create a map with some key-value pairs |
| 44 | + std::map<char, int> charMap; |
| 45 | + |
| 46 | + // Insert elements |
| 47 | + charMap['a'] = 10; |
| 48 | + charMap['b'] = 20; |
| 49 | + charMap['c'] = 30; |
| 50 | + |
| 51 | + // Iterate through the map using begin() and end() |
| 52 | + std::cout << "Map contents: " << std::endl; |
| 53 | + for (auto it = charMap.begin(); it != charMap.end(); ++it) { |
| 54 | + std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl; |
| 55 | + } |
| 56 | + |
| 57 | + return 0; |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +This code creates a map that associates characters with integers, then uses `.begin()` and `.end()` to establish the range for iteration. The loop continues until the iterator equals the value returned by `.end()`, which indicates we've gone through all elements. |
| 62 | + |
| 63 | +The output of this code will be: |
| 64 | + |
| 65 | +```shell |
| 66 | +Map contents: |
| 67 | +Key: a, Value: 10 |
| 68 | +Key: b, Value: 20 |
| 69 | +Key: c, Value: 30 |
| 70 | +``` |
| 71 | + |
| 72 | +## Example 2: Finding Elements in a Map with `.end()` |
| 73 | + |
| 74 | +This example shows how to use `.end()` as a comparison point to determine if an element exists in a map: |
| 75 | + |
| 76 | +```cpp |
| 77 | +#include <iostream> |
| 78 | +#include <map> |
| 79 | +#include <string> |
| 80 | + |
| 81 | +int main() { |
| 82 | + // Create a map for storing student grades |
| 83 | + std::map<std::string, char> studentGrades; |
| 84 | + |
| 85 | + // Add some student records |
| 86 | + studentGrades["Alice"] = 'A'; |
| 87 | + studentGrades["Bob"] = 'B'; |
| 88 | + studentGrades["Carol"] = 'A'; |
| 89 | + |
| 90 | + // Names to search for |
| 91 | + std::string names[] = {"Alice", "David", "Bob"}; |
| 92 | + |
| 93 | + // Search for each name in the map |
| 94 | + for (const auto& name : names) { |
| 95 | + // Use find() method which returns end() if element is not found |
| 96 | + auto it = studentGrades.find(name); |
| 97 | + |
| 98 | + if (it != studentGrades.end()) { |
| 99 | + // Element was found |
| 100 | + std::cout << name << "'s grade: " << it->second << std::endl; |
| 101 | + } else { |
| 102 | + // Element was not found (iterator equals end()) |
| 103 | + std::cout << name << " is not in the database." << std::endl; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +This example demonstrates a common pattern for checking if an element exists in a map. The `.find()` method returns an iterator to the element if found, or an iterator equal to `.end()` if not found. By comparing the returned iterator with `.end()`, we can determine if the search was successful. |
| 112 | + |
| 113 | +The output for this code will be: |
| 114 | + |
| 115 | +```shell |
| 116 | +Alice's grade: A |
| 117 | +David is not in the database. |
| 118 | +Bob's grade: B |
| 119 | +``` |
| 120 | + |
| 121 | +## Codebyte Example: Using `.end()` with STL Algorithms |
| 122 | + |
| 123 | +This example shows how to use `.end()` with STL algorithms to perform operations on map elements: |
| 124 | + |
| 125 | +```codebyte/cpp |
| 126 | +#include <iostream> |
| 127 | +#include <map> |
| 128 | +#include <algorithm> |
| 129 | +#include <string> |
| 130 | +
|
| 131 | +// Function to display map contents |
| 132 | +void printMap(const std::map<std::string, int>& m) { |
| 133 | + std::cout << "Map contents: " << std::endl; |
| 134 | + for (const auto& pair : m) { |
| 135 | + std::cout << pair.first << ": " << pair.second << std::endl; |
| 136 | + } |
| 137 | + std::cout << "------------------------" << std::endl; |
| 138 | +} |
| 139 | +
|
| 140 | +int main() { |
| 141 | + // Create a map of product prices |
| 142 | + std::map<std::string, int> productPrices; |
| 143 | +
|
| 144 | + // Add some products |
| 145 | + productPrices["Apple"] = 100; |
| 146 | + productPrices["Banana"] = 50; |
| 147 | + productPrices["Cherry"] = 120; |
| 148 | + productPrices["Date"] = 90; |
| 149 | +
|
| 150 | + // Display initial contents |
| 151 | + printMap(productPrices); |
| 152 | +
|
| 153 | + // Count items with price greater than 95 |
| 154 | + int expensiveCount = std::count_if( |
| 155 | + productPrices.begin(), |
| 156 | + productPrices.end(), |
| 157 | + [](const auto& pair) { return pair.second > 95; } |
| 158 | + ); |
| 159 | +
|
| 160 | + std::cout << "Number of products with price > 95: " << expensiveCount << std::endl; |
| 161 | +
|
| 162 | + // Apply discount to all products |
| 163 | + std::for_each( |
| 164 | + productPrices.begin(), |
| 165 | + productPrices.end(), |
| 166 | + [](auto& pair) { pair.second = static_cast<int>(pair.second * 0.9); } |
| 167 | + ); |
| 168 | +
|
| 169 | + // Display contents after discount |
| 170 | + std::cout << "After 10% discount:" << std::endl; |
| 171 | + printMap(productPrices); |
| 172 | +
|
| 173 | + return 0; |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +This example demonstrates how `.end()` is used with various STL algorithms. These algorithms require a range defined by two iterators, and the common pattern is to use `.begin()` and `.end()` to specify the entire container. |
| 178 | + |
| 179 | +To explore more C++ concepts, check out our [Learn C++](https://www.codecademy.com/learn/learn-c-plus-plus) course to enhance your coding skills. |
0 commit comments