-
Notifications
You must be signed in to change notification settings - Fork 4.3k
[Term Entry] C++ Unordered-sets : cend() #8099
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
Open
mamtawardhani
wants to merge
29
commits into
Codecademy:main
Choose a base branch
from
mamtawardhani:cend
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
af393a2
[Edit] Python: Python CLI arguments
mamtawardhani 17d337a
Update command-line-arguments.md
mamtawardhani 06bfd59
Merge branch 'Codecademy:main' into main
mamtawardhani e4aa0b1
Merge branch 'Codecademy:main' into main
mamtawardhani a25d227
Merge branch 'Codecademy:main' into main
mamtawardhani 10578df
[Term Entry] PyTorch Tensor Operations: .log2()
mamtawardhani 44eedef
Merge branch 'Codecademy:main' into main
mamtawardhani d23c8e3
Merge branch 'Codecademy:main' into main
mamtawardhani 8690381
Merge branch 'Codecademy:main' into main
mamtawardhani a8996b9
Merge branch 'Codecademy:main' into main
mamtawardhani f6a55c2
Merge branch 'Codecademy:main' into main
mamtawardhani 249f619
Merge branch 'Codecademy:main' into main
mamtawardhani 0b94a22
Merge branch 'Codecademy:main' into main
mamtawardhani fb43261
Merge branch 'Codecademy:main' into main
mamtawardhani 0fbbc39
[Term Entry] C++ Unordered-sets : cend()
mamtawardhani 5a74b04
Delete docs/content/pytorch/concepts/tensor-operations/terms/log2/log…
mamtawardhani 89e84d9
Apply suggestion from @avdhoottt
avdhoottt 8db31c6
Apply suggestion from @avdhoottt
avdhoottt 284797d
Apply suggestion from @avdhoottt
avdhoottt 1921259
Apply suggestion from @avdhoottt
avdhoottt 9e3a1d4
Apply suggestion from @avdhoottt
avdhoottt d058c55
Apply suggestion from @avdhoottt
avdhoottt 7b5631d
Apply suggestion from @avdhoottt
avdhoottt 80f2d62
Apply suggestion from @avdhoottt
avdhoottt c76fb1b
Apply suggestion from @avdhoottt
avdhoottt 9d95727
Apply suggestion from @avdhoottt
avdhoottt bdaeee6
Apply suggestion from @avdhoottt
avdhoottt 08cb53f
Apply suggestion from @avdhoottt
avdhoottt 2b0d29e
Merge branch 'main' into cend
avdhoottt 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
Some comments aren't visible on the classic Files Changed page.
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,137 @@ | ||
| --- | ||
| Title: 'cend()' | ||
| Description: 'Returns a constant iterator pointing just past the last element of the unordered set.' | ||
| Subjects: | ||
| - 'Computer Science' | ||
| - 'Game Development' | ||
| Tags: | ||
| - 'Functions' | ||
| - 'Iterators' | ||
| - 'Sets' | ||
| - 'STL' | ||
| CatalogContent: | ||
| - 'learn-c-plus-plus' | ||
| - 'paths/computer-science' | ||
| --- | ||
|
|
||
| The **`cend()`** method returns a constant iterator that points to the past-the-end position of an `unordered_set`. This iterator marks the end of the container’s range and cannot be dereferenced. | ||
|
|
||
| Because `unordered_set` does not maintain a defined order, iteration using [`cbegin()`](https://www.codecademy.com/resources/docs/cpp/unordered-set/cbegin) and `cend()` follows the container’s internal hash-table order. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ```pseudo | ||
| const_iterator cend() const noexcept; | ||
| ``` | ||
|
|
||
| Or, to work with a specific bucket: | ||
|
|
||
| ```pseudo | ||
| const_local_iterator cend(size_type n) const; | ||
| ``` | ||
|
|
||
| **Parameters:** | ||
|
|
||
| - `n` (size_type, optional): The bucket index. Must be less than `bucket_count()`. | ||
|
|
||
| **Return value:** | ||
|
|
||
| - No-parameter version: Returns a `const_iterator` pointing to the past-the-end position of the entire `unordered_set`. | ||
| - Bucket version: Returns a `const_local_iterator` pointing to the past-the-end position of bucket `n`. | ||
|
|
||
| ## Example 1: Iterating with constant iterators | ||
|
|
||
| In this example, `cbegin()` and `cend()` are used to iterate over an `unordered_set` without allowing modification of elements: | ||
|
|
||
| ```cpp | ||
| #include <iostream> | ||
| #include <unordered_set> | ||
| using namespace std; | ||
|
|
||
| int main() { | ||
| unordered_set<int> values = {4, 8, 15, 16, 23}; | ||
|
|
||
| for (auto it = values.cbegin(); it != values.cend(); ++it) { | ||
| cout << *it << "\n"; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| ``` | ||
|
|
||
| The output of this code is: | ||
|
|
||
| ```shell | ||
| 16 | ||
| 15 | ||
| 23 | ||
| 8 | ||
| 4 | ||
| ``` | ||
|
|
||
| > **Note:** For all code examples, the output order is implementation-dependent and may vary between different compilers, runs, or systems due to internal hash table organization. | ||
|
|
||
| ## Example 2: Using `cend()` with a bucket | ||
|
|
||
| In this example, `cend(n)` is used to mark the end of iteration for a specific bucket: | ||
|
|
||
| ```cpp | ||
| #include <iostream> | ||
| #include <unordered_set> | ||
| using namespace std; | ||
|
|
||
| int main() { | ||
| unordered_set<int> nums = {1, 2, 3, 4, 5}; | ||
|
|
||
| // Find which bucket contains the element 3 | ||
| size_t bucket = nums.bucket(3); | ||
|
|
||
| cout << "Elements in bucket " << bucket << ": "; | ||
| for (auto it = nums.cbegin(bucket); it != nums.cend(bucket); ++it) { | ||
| cout << *it << " "; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| ``` | ||
|
|
||
| The output of this code is: | ||
|
|
||
| ```shell | ||
| Elements in bucket 3: 3 | ||
| ``` | ||
|
|
||
| ## Codebyte Example | ||
|
|
||
| In this example, `cend()` is used to safely iterate through an `unordered_set` when only read access is required: | ||
|
|
||
| ```codebyte/cpp | ||
| #include <iostream> | ||
| #include <unordered_set> | ||
| using namespace std; | ||
|
|
||
| int main() { | ||
| unordered_set<string> animals = {"cat", "dog", "horse"}; | ||
|
|
||
| for (auto it = animals.cbegin(); it != animals.cend(); ++it) { | ||
| cout << *it << " "; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
| ## Frequently Asked Questions | ||
|
|
||
| ### 1. What is the `cend()` function in `unordered_set`? | ||
|
|
||
| The `cend()` function returns a constant iterator pointing to the position just past the last element of an `unordered_set`, marking the end of the container's range. | ||
|
|
||
| ### 2. Why use `unordered_set` in C++? | ||
|
|
||
| `unordered_set` provides fast average-case lookup, insertion, and deletion by using hash tables instead of ordered structures. | ||
|
|
||
| ### 3. Difference between end and cend? | ||
|
|
||
| Both `end()` and `cend()` return iterators that cannot modify elements (all iterators in `unordered_set` point to const elements). The difference is that `end()` returns an `iterator` or `const_iterator` depending on whether the container is const, while `cend()` always returns `const_iterator`. | ||
Oops, something went wrong.
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.