|
| 1 | +--- |
| 2 | +Title: '.find()' |
| 3 | +Description: 'Searches for an element with a particular key in a map.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Game Development' |
| 7 | +Tags: |
| 8 | + - 'Elements' |
| 9 | + - 'Map' |
| 10 | + - 'OOP' |
| 11 | + - 'Objects' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-c-plus-plus' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`.find()`** method searches for an element with a given key in a `std::map`. If the key exists, `.find()` returns an iterator pointing to the key-value pair; otherwise, it returns `map.end()`. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +mapName.find(key); |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `key`: The key to search for in the map. |
| 28 | + |
| 29 | +**Return value:** |
| 30 | + |
| 31 | +- If the key is found, returns an iterator pointing to the key-value pair. |
| 32 | +- If the key is not found, returns an iterator to `map.end()`. |
| 33 | + |
| 34 | +## Example |
| 35 | + |
| 36 | +This example demonstrates using `std::map` and the `.find()` method to check for an animal's existence and retrieve its lifespan efficiently: |
| 37 | + |
| 38 | +```cpp |
| 39 | +#include <iostream> |
| 40 | +#include <map> |
| 41 | + |
| 42 | +int main() { |
| 43 | + std::map<std::string, int> lifeSpan = { |
| 44 | + {"Giraffe", 26}, |
| 45 | + {"Goat", 15}, |
| 46 | + {"Lion", 10}, |
| 47 | + {"Tiger", 8} |
| 48 | + }; |
| 49 | + |
| 50 | + auto it = lifeSpan.find("Lion"); |
| 51 | + |
| 52 | + if (it != lifeSpan.end()) { |
| 53 | + std::cout << "Lion found! Lifespan: " << it->second << " years.\n"; |
| 54 | + } else { |
| 55 | + std::cout << "Lion not found in the map.\n"; |
| 56 | + } |
| 57 | + |
| 58 | + return 0; |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +The code above results in the following output: |
| 63 | + |
| 64 | +```shell |
| 65 | +Lion found! Lifespan: 10 years. |
| 66 | +``` |
| 67 | + |
| 68 | +## Codebyte Example |
| 69 | + |
| 70 | +Run the following codebyte example to understand how to use `.find()` to search for a key in a map: |
| 71 | + |
| 72 | +```codebyte/cpp |
| 73 | +#include <iostream> |
| 74 | +#include <map> |
| 75 | +
|
| 76 | +int main() { |
| 77 | + // Initializing map with items |
| 78 | + std::map<std::string, int> lifeSpan = { |
| 79 | + {"Giraffe", 26}, |
| 80 | + {"Goat", 15}, |
| 81 | + {"Lion", 10}, |
| 82 | + {"Tiger", 8} |
| 83 | + }; |
| 84 | +
|
| 85 | + // Searching for a key |
| 86 | + std::string key = "Lion"; |
| 87 | + auto it = lifeSpan.find(key); |
| 88 | +
|
| 89 | + // Checking if the key was found |
| 90 | + if (it != lifeSpan.end()) { |
| 91 | + std::cout << key << " found! Lifespan: " << it->second << " years.\n"; |
| 92 | + } else { |
| 93 | + std::cout << key << " not found in the map.\n"; |
| 94 | + } |
| 95 | +
|
| 96 | + // Searching for a non-existent key |
| 97 | + key = "Elephant"; |
| 98 | + it = lifeSpan.find(key); |
| 99 | +
|
| 100 | + if (it != lifeSpan.end()) { |
| 101 | + std::cout << key << " found! Lifespan: " << it->second << " years.\n"; |
| 102 | + } else { |
| 103 | + std::cout << key << " not found in the map.\n"; |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
0 commit comments