Skip to content

Commit c740444

Browse files
[Term Entry] C++ Deque: size() (#7975)
* [Edit] Python: Python CLI arguments * Update command-line-arguments.md * [Term Entry] PyTorch Tensor Operations: .log2() * [Term Entry] C++ Deque: size() * Delete docs/content/pytorch/concepts/tensor-operations/terms/log2/log2.md * Apply suggestion from @avdhoottt * Apply suggestion from @avdhoottt * Apply suggestion from @avdhoottt * Apply suggestion from @avdhoottt ---------
1 parent 4c50c1a commit c740444

File tree

1 file changed

+125
-0
lines changed
  • content/cpp/concepts/deque/terms/size

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
Title: 'size()'
3+
Description: 'Returns the number of elements in a deque container.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Game Development'
7+
Tags:
8+
- 'Containers'
9+
- 'Deques'
10+
- 'Methods'
11+
- 'STL'
12+
CatalogContent:
13+
- 'learn-c-plus-plus'
14+
- 'paths/computer-science'
15+
---
16+
17+
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.
18+
19+
## Syntax
20+
21+
```pseudo
22+
deque_object.size();
23+
```
24+
25+
**Parameters:**
26+
27+
This function does not take any parameters.
28+
29+
**Return value:**
30+
31+
Returns a value of type `size_type` (an unsigned integral type) representing the number of elements in the deque.
32+
33+
## Example 1
34+
35+
In this example, the size method is used to check the element count of an initially empty deque, then after [`push_back`](https://www.codecademy.com/resources/docs/cpp/deque/push-back) operations:
36+
37+
```cpp
38+
#include <deque>
39+
#include <iostream>
40+
41+
int main() {
42+
std::deque<int> d;
43+
std::cout << "Initial size: " << d.size() << "\n";
44+
45+
for (int i = 0; i < 5; ++i) {
46+
d.push_back(i);
47+
}
48+
std::cout << "Size after push_back 5 elements: " << d.size() << "\n";
49+
50+
return 0;
51+
}
52+
```
53+
54+
The output of this code is:
55+
56+
```shell
57+
Initial size: 0
58+
Size after push_back 5 elements: 5
59+
```
60+
61+
## Example 2
62+
63+
In this example, the size method is used after insert and pop operations to illustrate dynamic changes in element count:
64+
65+
```cpp
66+
#include <deque>
67+
#include <iostream>
68+
69+
int main() {
70+
std::deque<int> d = {1, 2, 3};
71+
std::cout << "Initial size: " << d.size() << "\n";
72+
73+
d.pop_front();
74+
d.pop_back();
75+
std::cout << "Size after two pops: " << d.size() << "\n";
76+
77+
d.insert(d.begin(), 10);
78+
std::cout << "Size after one insert at front: " << d.size() << "\n";
79+
80+
return 0;
81+
}
82+
```
83+
84+
The output of this code is:
85+
86+
```shell
87+
Initial size: 3
88+
Size after two pops: 1
89+
Size after one insert at front: 2
90+
```
91+
92+
## Codebyte Example
93+
94+
In this example, the `size()` method is repeatedly checked in a loop until the deque becomes empty:
95+
96+
```codebyte/cpp
97+
#include <iostream>
98+
#include <deque>
99+
100+
int main() {
101+
std::deque<char> letters = {'A', 'B', 'C', 'D', 'E'};
102+
103+
while (!letters.empty()) {
104+
std::cout << "Current size: " << letters.size() << " front element: " << letters.front() << "\n";
105+
letters.pop_front();
106+
}
107+
108+
std::cout << "Final size after emptying: " << letters.size() << "\n";
109+
return 0;
110+
}
111+
```
112+
113+
## Frequently Asked Questions
114+
115+
### 1. What does `size()` do in C++?
116+
117+
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.
118+
119+
### 2. What is a deque function in C++?
120+
121+
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.
122+
123+
### 3. How to get the size of a deque in C++?
124+
125+
You can get the number of elements in a deque using the `size()` member function.

0 commit comments

Comments
 (0)