-
Notifications
You must be signed in to change notification settings - Fork 4.3k
[Term Entry] C++ Deque: size() #7975
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 11 commits
Commits
Show all changes
15 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 6f776ae
[Term Entry] C++ Deque: size()
mamtawardhani c7f2c41
Delete docs/content/pytorch/concepts/tensor-operations/terms/log2/log…
mamtawardhani 3fbd508
Apply suggestion from @avdhoottt
avdhoottt 80bfece
Apply suggestion from @avdhoottt
avdhoottt b737be7
Apply suggestion from @avdhoottt
avdhoottt fc7e422
Apply suggestion from @avdhoottt
avdhoottt e7342ec
Merge branch 'main' into deque-size
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
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,125 @@ | ||
| --- | ||
| Title: 'size()' | ||
| Description: 'Returns the number of elements in a deque container.' | ||
| Subjects: | ||
| - 'Computer Science' | ||
| - 'Game Development' | ||
| Tags: | ||
| - 'Containers' | ||
| - 'Deques' | ||
| - 'Methods' | ||
| - 'STL' | ||
| CatalogContent: | ||
| - 'learn-c-plus-plus' | ||
| - 'paths/computer-science' | ||
| --- | ||
|
|
||
| The **`size()`** function returns the number of elements currently stored in a [`std::deque`](https://www.codecademy.com/resources/docs/cpp/deque) container. It has constant time complexity (`O(1)`) and is marked noexcept since C++ 11. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ```pseudo | ||
| deque_object.size(); | ||
| ``` | ||
|
|
||
| **Parameters:** | ||
|
|
||
| This function does not take any parameters. | ||
|
|
||
| **Return value:** | ||
|
|
||
| Returns a value of type `size_type` (an unsigned integral type) representing the number of elements in the deque. | ||
|
|
||
| ## Example 1 | ||
|
|
||
| In this example, the size method is used to check the element count of an initially empty deque, then after `push_back` operations: | ||
|
|
||
| ```cpp | ||
| #include <deque> | ||
| #include <iostream> | ||
|
|
||
| int main() { | ||
| std::deque<int> d; | ||
| std::cout << "Initial size: " << d.size() << "\n"; | ||
|
|
||
| for (int i = 0; i < 5; ++i) { | ||
| d.push_back(i); | ||
| } | ||
| std::cout << "Size after push_back 5 elements: " << d.size() << "\n"; | ||
|
|
||
| return 0; | ||
| } | ||
| ``` | ||
|
|
||
| The output of this code is: | ||
|
|
||
| ```shell | ||
| Initial size: 0 | ||
| Size after push_back 5 elements: 5 | ||
| ``` | ||
|
|
||
| ## Example 2 | ||
|
|
||
| In this example, the size method is used after insert and pop operations to illustrate dynamic changes in element count: | ||
|
|
||
| ```cpp | ||
| #include <deque> | ||
| #include <iostream> | ||
|
|
||
| int main() { | ||
| std::deque<int> d = {1, 2, 3}; | ||
| std::cout << "Initial size: " << d.size() << "\n"; | ||
|
|
||
| d.pop_front(); | ||
| d.pop_back(); | ||
| std::cout << "Size after two pops: " << d.size() << "\n"; | ||
|
|
||
| d.insert(d.begin(), 10); | ||
| std::cout << "Size after one insert at front: " << d.size() << "\n"; | ||
|
|
||
| return 0; | ||
| } | ||
| ``` | ||
|
|
||
| The output of this code is: | ||
|
|
||
| ```shell | ||
| Initial size: 3 | ||
| Size after two pops: 1 | ||
| Size after one insert at front: 2 | ||
| ``` | ||
|
|
||
| ## Codebyte Example | ||
|
|
||
| In this example, the size method is repeatedly checked in a loop until the deque becomes empty: | ||
avdhoottt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```codebyte/cpp | ||
| #include <iostream> | ||
| #include <deque> | ||
|
|
||
| int main() { | ||
| std::deque<char> letters = {'A', 'B', 'C', 'D', 'E'}; | ||
|
|
||
| while (!letters.empty()) { | ||
| std::cout << "Current size: " << letters.size() << " front element: " << letters.front() << "\n"; | ||
| letters.pop_front(); | ||
| } | ||
|
|
||
| std::cout << "Final size after emptying: " << letters.size() << "\n"; | ||
| return 0; | ||
| } | ||
| ``` | ||
|
|
||
| ## Frequently Asked Questions | ||
|
|
||
| ### 1. What does `size()` do in C++? | ||
|
|
||
| The `size()` function in C++ returns the number of elements present in a container, such as a `std::deque`, `std::vector`, or `std::string`. It gives the current length of the container in constant time (O(1)) without modifying it. | ||
|
|
||
| ### 2. What is a deque function in C++? | ||
|
|
||
| A deque (double-ended queue) in C++ is a Standard Template Library (STL) container that allows insertion and deletion of elements from both the front and back efficiently. | ||
|
|
||
| ### 3. How to get the size of a deque in C++? | ||
|
|
||
| You can get the number of elements in a deque using the `size()` member function. | ||
avdhoottt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.