Skip to content

Commit f865d96

Browse files
Merge branch 'main' into plot
2 parents 67773fc + 48667b8 commit f865d96

File tree

4 files changed

+341
-1
lines changed

4 files changed

+341
-1
lines changed

bin/concept-of-the-week.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
content/kotlin/concepts/strings/strings.md
1+
content/kotlin/concepts/classes/classes.md
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.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
Title: 'emplace()'
3+
Description: 'Constructs a new element at the end of the queue in-place using forwarded arguments.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Containers'
9+
- 'Methods'
10+
- 'Queues'
11+
CatalogContent:
12+
- 'learn-c-plus-plus'
13+
- 'paths/computer-science'
14+
---
15+
16+
The **`emplace()`** method of the `std::queue<T,Container>` container adaptor constructs a new element directly in the underlying container at the back of the queue, forwarding the provided arguments to the constructor of `T`. Because the object is constructed in place, this can improve performance for types with expensive copy or move operations.
17+
18+
## Syntax
19+
20+
```pseudo
21+
queue.emplace(args...)
22+
```
23+
24+
**Parameters:**
25+
26+
- `args...` (variadic template parameters): Arguments forwarded to construct the new element of type `T`.
27+
28+
**Return value:**
29+
30+
Since C++17: A reference to the inserted element (the value returned by the underlying container's `emplace_back` method).
31+
32+
## Example 1: Enqueueing log entries into a queue
33+
34+
In this example, log messages are constructed in place and enqueued for later processing:
35+
36+
```cpp
37+
#include <iostream>
38+
#include <queue>
39+
#include <string>
40+
41+
struct LogEntry {
42+
std::string level;
43+
std::string message;
44+
LogEntry(std::string lvl, std::string msg)
45+
: level(std::move(lvl)), message(std::move(msg)) {}
46+
};
47+
48+
int main(){
49+
std::queue<LogEntry> logs;
50+
logs.emplace("INFO", "Application started");
51+
logs.emplace("WARN", "Low disk space");
52+
logs.emplace("ERROR", "Out of memory");
53+
54+
while(!logs.empty()){
55+
const auto& entry = logs.front();
56+
std::cout << "[" << entry.level << "] " << entry.message << "\n";
57+
logs.pop();
58+
}
59+
}
60+
```
61+
62+
The output of this code is:
63+
64+
```shell
65+
[INFO] Application started
66+
[WARN] Low disk space
67+
[ERROR] Out of memory
68+
```
69+
70+
## Example 2: Constructing tasks in a task queue
71+
72+
In this example, tasks with multiple constructor parameters are constructed directly inside the queue:
73+
74+
```cpp
75+
#include <iostream>
76+
#include <queue>
77+
#include <functional>
78+
79+
struct Task {
80+
int id;
81+
std::string description;
82+
Task(int i, std::string desc)
83+
: id(i), description(std::move(desc)) {}
84+
void run() const { std::cout << "Running task #" << id << ": " << description << "\n"; }
85+
};
86+
87+
int main(){
88+
std::queue<Task> taskQueue;
89+
taskQueue.emplace(1, "Load configuration");
90+
taskQueue.emplace(2, "Initialize modules");
91+
taskQueue.emplace(3, "Start services");
92+
93+
while(!taskQueue.empty()){
94+
taskQueue.front().run();
95+
taskQueue.pop();
96+
}
97+
}
98+
```
99+
100+
The output of this code is:
101+
102+
```shell
103+
Running task #1: Load configuration
104+
Running task #2: Initialize modules
105+
Running task #3: Start services
106+
```
107+
108+
## Codebyte Example: Buffering sensor data with emplace
109+
110+
In this example, sensor readings are constructed and enqueued as soon as they arrive, minimizing overhead:
111+
112+
```codebyte/cpp
113+
#include <iostream>
114+
#include <queue>
115+
#include <tuple>
116+
117+
struct SensorData {
118+
int sensorId;
119+
double value;
120+
long timestamp;
121+
SensorData(int id, double val, long ts)
122+
: sensorId(id), value(val), timestamp(ts) {}
123+
void print() const {
124+
std::cout << "Sensor#" << sensorId
125+
<< " value=" << value
126+
<< " time=" << timestamp << "\n";
127+
}
128+
};
129+
130+
int main(){
131+
std::queue<SensorData> buffer;
132+
buffer.emplace(101, 23.5, 1617181920L);
133+
buffer.emplace(102, 19.8, 1617181930L);
134+
135+
while(!buffer.empty()){
136+
buffer.front().print();
137+
buffer.pop();
138+
}
139+
140+
return 0;
141+
}
142+
```
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
Title: '.lastIndexOf()'
3+
Description: 'Returns the index of the last occurrence of a specified substring within a string, or -1 if the substring is not found.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Web Development'
7+
Tags:
8+
- 'Strings'
9+
- 'Methods'
10+
- 'JavaScript'
11+
CatalogContent:
12+
- 'learn-javascript'
13+
- 'paths/web-development'
14+
---
15+
16+
The **`.lastIndexOf()`** method in JavaScript returns the position of the last occurrence of a specified substring within a [string](https://www.codecademy.com/resources/docs/javascript/strings). If the substring is not found, it returns `-1`. It performs a case-sensitive search and can take an optional starting position from which to begin the search backwards.
17+
18+
## Syntax
19+
20+
```pseudo
21+
string.lastIndexOf(searchValue, fromIndex)
22+
```
23+
24+
**Parameters:**
25+
26+
- `searchValue`: The substring to search for.
27+
- `fromIndex` (optional): The position to start searching backward from. Defaults to the string’s length.
28+
29+
**Return value:**
30+
31+
Returns an integer representing the index of the last occurrence of the specified substring within the string. If the substring isn’t found, it returns `-1`.
32+
33+
## Example 1: Finding the Last Mention of a Name
34+
35+
In this example, a chat message contains multiple mentions of a person’s name, and the method finds the last one:
36+
37+
```js
38+
const message = 'Hey Sam, Sam, are you coming to the meeting, Sam?';
39+
const lastSam = message.lastIndexOf('Sam');
40+
console.log(lastSam);
41+
```
42+
43+
The output of this code is:
44+
45+
```shell
46+
45
47+
```
48+
49+
## Example 2: Searching Backward from a Certain Point
50+
51+
In this example, the search begins from a specific index to locate the previous occurrence of a keyword:
52+
53+
```js
54+
const report = 'Error at line 23. Warning at line 45. Error again at line 78.';
55+
const prevError = report.lastIndexOf('Error', 40);
56+
console.log(prevError);
57+
```
58+
59+
The output of this code is:
60+
61+
```shell
62+
38
63+
```
64+
65+
## Codebyte Example: Locating the Last Hashtag in a Social Media Caption
66+
67+
In this example, a caption includes multiple hashtags, and the method identifies the position of the last one:
68+
69+
```codebyte/javascript
70+
const caption = 'Just finished a 10k run! #fitness #running #motivation';
71+
const lastHashtag = caption.lastIndexOf('#');
72+
console.log(lastHashtag);
73+
```

0 commit comments

Comments
 (0)