-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
[Term Entry] C++ sets - .find() #6141
base: main
Are you sure you want to change the base?
Conversation
…s.md Deleted test file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Radhika-okhade, thanks for contributing to docs. I have a few suggestions, please check the comments and make the necessary changes. Let me know if you need any help or have any questions.
--- | ||
|
||
The **`.find()`** method in C++ searches for a specific element in a `std::set`. If the element is found, `.find()` returns an iterator pointing to the element; otherwise, it returns an iterator to `set.end()`. This method is part of the Standard Template Library (STL). | ||
It allows efficient lookups in `std::set`, which is typically implemented as a balanced binary search tree. This ensures average **O(log n)** time complexity for lookups. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The last sentence can be rewritten like this for better readability -
It allows efficient lookups in `std::set`, which is typically implemented as a balanced binary search tree. This ensures average **O(log n)** time complexity for lookups. | |
It allows efficient lookups in `std::set`, which is typically implemented as a balanced binary search tree. This ensures an average time complexity of **`O(log n)`** for lookups. |
- If the element is found, it returns an iterator pointing to the element. | ||
- If the element is not found, it returns an iterator to `set.end()`. | ||
|
||
Before diving into real-world applications, let's start with a generic example that demonstrates the fundamental usage of `.find()` in a `std::set`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can skip this sentence as we usually don't write such transition sentences in docs in an attempt to keep it very short and direct.
Before diving into real-world applications, let's start with a generic example that demonstrates the fundamental usage of `.find()` in a `std::set`. |
|
||
Before diving into real-world applications, let's start with a generic example that demonstrates the fundamental usage of `.find()` in a `std::set`. | ||
|
||
## Example 1 - How to find an element in an array using `.find()` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should keep this heading as ## Examples
and the subheading could be 'Finding an element in an std::set
using .find()
'. Also, the method applies to std::set
, not an array.
## Example 1 - How to find an element in an array using `.find()` | |
## Example | |
### Finding an element in a `std::set` using `.find()` |
Element found: 30 | ||
``` | ||
|
||
Now that we've seen a basic example, let's explore practical use cases of `.find()`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can again skip this transition sentence.
Now that we've seen a basic example, let's explore practical use cases of `.find()`. |
|
||
Now that we've seen a basic example, let's explore practical use cases of `.find()`. | ||
|
||
## Example 2 - Checking if a Product Exists in Inventory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following the above structure, this would be a subheading under ## Examples
.
## Example 2 - Checking if a Product Exists in Inventory | |
### Checking if a Product Exists in Inventory |
```cpp | ||
#include <iostream> | ||
#include <set> | ||
#include <string> | ||
|
||
struct CompareIgnoreCase { | ||
|
||
bool operator()(const std::string &a, const std::string &b) const { | ||
return a < b; | ||
|
||
} | ||
|
||
}; | ||
|
||
int main() { | ||
|
||
std::set<std::string, CompareIgnoreCase> names = {"Alice", "Bob", "Charlie"}; | ||
std::string searchName = "Bob"; | ||
auto it = names.find(searchName); | ||
|
||
if (it != names.end()) { | ||
|
||
std::cout << "Name found: " << *it << std::endl; | ||
|
||
} else { | ||
|
||
std::cout << "Name not found." << std::endl; | ||
|
||
} | ||
|
||
return 0; | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix indentation. The code blocks should only have one space indentation.
### Output | ||
|
||
```shell | ||
Name found: Bob | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above comment - Please make this part uniform as the above example, preferred format would be to keep it as **Output**
or to just write it as a sentence like - The output will be:
.
} | ||
``` | ||
|
||
To explore more about C++ sets, iterators, and the Standard Template Library, check out Codecademy's [C++ for Programmers]( https://www.codecademy.com/learn/c-plus-plus-for-programmers) course. This course provides in-depth lessons and hands-on practice to strengthen your programming skills. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can skip this part as we usually don't include course information in doc entries like this.
To explore more about C++ sets, iterators, and the Standard Template Library, check out Codecademy's [C++ for Programmers]( https://www.codecademy.com/learn/c-plus-plus-for-programmers) course. This course provides in-depth lessons and hands-on practice to strengthen your programming skills. |
#include <iostream> | ||
#include <set> | ||
|
||
int main() { | ||
|
||
std::set<int> numbers = {100, 200, 300, 400}; | ||
int searchValue = 200; | ||
auto it = numbers.find(searchValue); | ||
|
||
if (it != numbers.end()) { | ||
|
||
std::cout << "Found: " << *it << std::endl; | ||
|
||
} else { | ||
|
||
std::cout << "Not found." << std::endl; | ||
|
||
} | ||
|
||
return 0; | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix indentation. The code blocks should only have one space indentation.
|
||
## Codebyte Example | ||
|
||
This example demonstrates a simple use of `.find()` in a C++ set. It checks for the presence of a specific integer in a set and prints whether the element was found or not. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.find()
only works with std::set
This example demonstrates a simple use of `.find()` in a C++ set. It checks for the presence of a specific integer in a set and prints whether the element was found or not. | |
This example demonstrates a simple use of `.find()` in a C++ STL set. It checks for the presence of a specific integer in a set and prints whether the element was found or not. |
Description
Added new term entry on
.find()
Issue Solved
#6091
Type of Change
Checklist
main
branch.Issues Solved
section.