-
Notifications
You must be signed in to change notification settings - Fork 4.1k
[Term Entry] C++ Maps: .find() #6386
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
1fddaf1
Add formatting scripts
dancikmad 519d198
Merge remote-tracking branch 'upstream/main'
dancikmad ca057bf
Merge remote-tracking branch 'upstream/main'
dancikmad d133d7a
Merge remote-tracking branch 'upstream/main'
dancikmad ef940a8
Merge remote-tracking branch 'upstream/main'
dancikmad 914dcdb
Merge branch 'Codecademy:main' into main
dancikmad 765f05d
feat: add documentation entry for C++ Map.find() method
dancikmad cc538c1
Fix formatting and ran scripts
dancikmad a9805d1
Update package.json
mamtawardhani 229d26a
Update yarn.lock
mamtawardhani edc66a3
Merge branch 'main' into cpp-map-find
mamtawardhani fe82ad2
Merge branch 'main' into cpp-map-find
dancikmad c93d152
fix: addressed PR review comments
dancikmad 39aca8a
refactor: run linting and format code for final review
dancikmad 6f8391c
Merge branch 'main' into cpp-map-find
dancikmad c1d9828
feat: add an ## Example code for Maps. find() method
dancikmad 964135d
[Term Entry] C++ Maps: .find() #6386
dancikmad 081dceb
minor fixes
mamtawardhani 005d63d
Merge branch 'main' into cpp-map-find
mamtawardhani 857dda6
Merge branch 'main' into cpp-map-find
Sriparno08 d433840
Update find.md
Sriparno08 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
--- | ||
Title: '.find()' | ||
Description: 'Searches for an element with a particular key in a map.' | ||
Subjects: | ||
- 'Computer Science' | ||
- 'Game Development' | ||
Tags: | ||
- 'Elements' | ||
- 'Map' | ||
- 'OOP' | ||
- 'Objects' | ||
CatalogContent: | ||
- 'learn-c-plus-plus' | ||
- 'paths/computer-science' | ||
--- | ||
|
||
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()`. | ||
|
||
## Syntax | ||
|
||
```pseudo | ||
mapName.find(key); | ||
``` | ||
|
||
**Parameters:** | ||
|
||
- `key`: The key to search for in the map. | ||
|
||
**Return value:** | ||
|
||
- If the key is found, returns an iterator pointing to the key-value pair. | ||
- If the key is not found, returns an iterator to `map.end()`. | ||
|
||
## Example | ||
|
||
This example demonstrates using `std::map` and the `.find()` method to check for an animal's existence and retrieve its lifespan efficiently: | ||
|
||
```cpp | ||
#include <iostream> | ||
#include <map> | ||
|
||
int main() { | ||
std::map<std::string, int> lifeSpan = { | ||
{"Giraffe", 26}, | ||
{"Goat", 15}, | ||
{"Lion", 10}, | ||
{"Tiger", 8} | ||
}; | ||
|
||
auto it = lifeSpan.find("Lion"); | ||
|
||
if (it != lifeSpan.end()) { | ||
std::cout << "Lion found! Lifespan: " << it->second << " years.\n"; | ||
} else { | ||
std::cout << "Lion not found in the map.\n"; | ||
} | ||
|
||
return 0; | ||
} | ||
``` | ||
|
||
The code above results in the following output: | ||
|
||
```shell | ||
Lion found! Lifespan: 10 years. | ||
``` | ||
|
||
## Codebyte Example | ||
|
||
Run the following codebyte example to understand how to use `.find()` to search for a key in a map: | ||
|
||
```codebyte/cpp | ||
#include <iostream> | ||
#include <map> | ||
|
||
int main() { | ||
// Initializing map with items | ||
std::map<std::string, int> lifeSpan = { | ||
{"Giraffe", 26}, | ||
{"Goat", 15}, | ||
{"Lion", 10}, | ||
{"Tiger", 8} | ||
}; | ||
|
||
// Searching for a key | ||
std::string key = "Lion"; | ||
auto it = lifeSpan.find(key); | ||
|
||
// Checking if the key was found | ||
if (it != lifeSpan.end()) { | ||
std::cout << key << " found! Lifespan: " << it->second << " years.\n"; | ||
} else { | ||
std::cout << key << " not found in the map.\n"; | ||
} | ||
|
||
// Searching for a non-existent key | ||
key = "Elephant"; | ||
it = lifeSpan.find(key); | ||
|
||
if (it != lifeSpan.end()) { | ||
std::cout << key << " found! Lifespan: " << it->second << " years.\n"; | ||
} else { | ||
std::cout << key << " not found in the map.\n"; | ||
} | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.