Skip to content
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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

Radhika-okhade
Copy link
Collaborator

Description

Added new term entry on .find()

Issue Solved

#6091

Type of Change

  • Adding a new entry

Checklist

  • [/ ] All writings are my own.
  • [ /] My entry follows the Codecademy Docs style guide.
  • [/ ] My changes generate no new warnings.
  • [ /] I have performed a self-review of my own writing and code.
  • [/ ] I have checked my entry and corrected any misspellings.
  • [/ ] I have made corresponding changes to the documentation if needed.
  • [/ ] I have confirmed my changes are not being pushed from my forked main branch.
  • [/ ] I have confirmed that I'm pushing from a new branch named after the changes I'm making.
  • [/ ] I have linked any issues that are relevant to this PR in the Issues Solved section.

Copy link
Collaborator

@PragatiVerma18 PragatiVerma18 left a 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.
Copy link
Collaborator

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 -

Suggested change
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`.
Copy link
Collaborator

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.

Suggested change
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()`
Copy link
Collaborator

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.

Suggested change
## 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()`.
Copy link
Collaborator

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.

Suggested change
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
Copy link
Collaborator

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.

Suggested change
## Example 2 - Checking if a Product Exists in Inventory
### Checking if a Product Exists in Inventory

Comment on lines +108 to +140
```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;

}
Copy link
Collaborator

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.

Comment on lines +143 to +147
### Output

```shell
Name found: Bob
```
Copy link
Collaborator

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.
Copy link
Collaborator

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.

Suggested change
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.

Comment on lines +154 to +175
#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;

}
Copy link
Collaborator

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.
Copy link
Collaborator

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

Suggested change
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants