From 00bd3e1ff5e2d94556f77e5f50ae8555ec640856 Mon Sep 17 00:00:00 2001 From: JayArindamMaity Date: Wed, 26 Nov 2025 10:21:12 +0000 Subject: [PATCH 1/2] feat: add documentation for unordered_set find method --- .../concepts/unordered-set/terms/find/find.md | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 content/cpp/concepts/unordered-set/terms/find/find.md diff --git a/content/cpp/concepts/unordered-set/terms/find/find.md b/content/cpp/concepts/unordered-set/terms/find/find.md new file mode 100644 index 00000000000..51164ceb975 --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/find/find.md @@ -0,0 +1,108 @@ +--- +Title: '.find()' +Description: 'Searches the container for an element with a specific key and returns an iterator to it if found.' +Subjects: + - 'Computer Science' + - 'Programming' +Tags: + - 'C++' + - 'Unordered Set' + - 'STL' + - 'Methods' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The **`.find()`** method searches an `std::unordered_set` for a specific +key. + +If the key is found, `.find()` returns an iterator pointing to that +element. If it is not found, the method returns an iterator equal to +`unordered_set::end()`, representing the position past the last element +in the container. + +## Syntax + +```pseudo +set_name.find(key); +``` + +**Parameters:** + +- `key`: The value to search for in the `unordered_set`. + +**Return value:** + +Returns an iterator pointing to the matching element if found.\ +If the key is not present, it returns an iterator equal to `end()`. + +## Example + +The example below demonstrates using `.find()` with an `unordered_set`: + +```cpp +#include +#include +#include + +int main() { + std::unordered_set fruits = {"apple", "banana", "cherry"}; + + // Search for "banana" + auto search = fruits.find("banana"); + + if (search != fruits.end()) { + std::cout << "Found: " << *search << "\n"; + } else { + std::cout << "Not found\n"; + } + + // Search for "grape" + if (fruits.find("grape") == fruits.end()) { + std::cout << "grape not found in the set.\n"; + } + + return 0; +} +``` + +The output of this code is: + +```shell +Found: banana +grape not found in the set. +``` + +## Codebyte Example + +The following runnable example shows how to use `.find()` to look up +elements in an `unordered_set`: + +```codebyte/cpp +#include +#include +#include + +int main() { + std::unordered_set colors = {"red", "green", "blue", "yellow"}; + + // Search for "green" + auto it = colors.find("green"); + + if (it != colors.end()) { + std::cout << "Found: " << *it << "\n"; + } else { + std::cout << "Not found\n"; + } + + // Attempt to search for a missing color + auto missing = colors.find("purple"); + + if (missing == colors.end()) { + std::cout << "purple not found in the set.\n"; + } + + return 0; +} +``` From bdc67f3bcb7efc3b9869f7af10fa9a9cae3dcbca Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 3 Dec 2025 17:59:00 +0530 Subject: [PATCH 2/2] minor content fixes --- .../concepts/unordered-set/terms/find/find.md | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/find/find.md b/content/cpp/concepts/unordered-set/terms/find/find.md index 51164ceb975..8ec2dc55550 100644 --- a/content/cpp/concepts/unordered-set/terms/find/find.md +++ b/content/cpp/concepts/unordered-set/terms/find/find.md @@ -3,24 +3,17 @@ Title: '.find()' Description: 'Searches the container for an element with a specific key and returns an iterator to it if found.' Subjects: - 'Computer Science' - - 'Programming' + - 'Game Development' Tags: - - 'C++' - - 'Unordered Set' - - 'STL' - 'Methods' + - 'Sets' + - 'STL' CatalogContent: - 'learn-c-plus-plus' - 'paths/computer-science' --- -The **`.find()`** method searches an `std::unordered_set` for a specific -key. - -If the key is found, `.find()` returns an iterator pointing to that -element. If it is not found, the method returns an iterator equal to -`unordered_set::end()`, representing the position past the last element -in the container. +The **`.find()`** method searches an `std::unordered_set` for a specific key. If the key is found, the method returns an iterator pointing to that element. If the key is not found, it returns an iterator equal to `unordered_set::end()`, which represents the past-the-end position in the container. ## Syntax @@ -34,10 +27,11 @@ set_name.find(key); **Return value:** -Returns an iterator pointing to the matching element if found.\ -If the key is not present, it returns an iterator equal to `end()`. +Returns an iterator to the element with a key equivalent to key, if such an element exists. Otherwise, it returns an iterator equal to `end()`. -## Example +## Example: Using `.find()` to Locate an Existing and Missing Key + +In this example, `.find()` checks whether specific fruit names exist in the set: The example below demonstrates using `.find()` with an `unordered_set`: @@ -74,7 +68,9 @@ Found: banana grape not found in the set. ``` -## Codebyte Example +## Codebyte Example: Using `.find()` to Validate Presence of Colors + +In this example, `.find()` tests for a valid color and then checks a color that does not exist: The following runnable example shows how to use `.find()` to look up elements in an `unordered_set`: