From 3e4bf676c8d66384abebbb656ffa141f35cc9ab6 Mon Sep 17 00:00:00 2001 From: Fejiro001 Date: Wed, 30 Oct 2024 16:10:20 +0100 Subject: [PATCH 1/2] New Term Entry for C++ Deque Clear() method --- .../cpp/concepts/deque/terms/clear/clear.md | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 content/cpp/concepts/deque/terms/clear/clear.md diff --git a/content/cpp/concepts/deque/terms/clear/clear.md b/content/cpp/concepts/deque/terms/clear/clear.md new file mode 100644 index 00000000000..5af50f25803 --- /dev/null +++ b/content/cpp/concepts/deque/terms/clear/clear.md @@ -0,0 +1,98 @@ +--- +Title: '.clear()' +Description: 'Removes all elements from the deque.' +Subjects: + - 'Computer Science' + - 'Game Development' +Tags: + - 'Classes' + - 'Containers' + - 'Deques' + - 'OOP' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +In C++, the **`.clear()`** [method](https://www.codecademy.com/resources/docs/cpp/methods) removes all elements from the deque container, leaving the container with a size of 0. + +## Syntax + +```pseudo +dequeName.clear(); +``` + +- Has no parameters passed. + +## Example + +The example below showcases the use of the `.clear()` method: + +```cpp +#include +#include + +int main() { + // Create a deque of integers + std::deque numbers; + + // Add elements to the deque + numbers.push_back(50); + numbers.push_back(100); + numbers.push_back(150); + numbers.push_back(200); + + // Display the elements of the deque + std::cout << "My deque contains: "; + for (int num : numbers) { + std::cout << num << " "; + } + + numbers.clear(); + std::cout << std::endl; + + numbers.push_back(200); + + std::cout << "My deque contains: "; + for (int num : numbers) { + std::cout << num << " "; + } + + std::cout << std::endl; + + return 0; +} +``` + +The above code generates the following output: + +```shell +My deque contains: 50 100 150 200 +My deque contains: 200 +``` + +## Codebyte Example + +The following codebyte removes all values prior to calling the `.clear()` method. +Run the following example to understand the use of the `.clear()` method: + +```codebyte/cpp +#include +#include +#include + +int main() { + std::deque myDeque; + + myDeque.push_back("A"); + myDeque.push_back("B"); + myDeque.push_back("C"); + + myDeque.clear(); + myDeque.push_back("D"); + + for (const auto& value : myDeque) { + std::cout << ' ' << value; + } +} +``` From eb3ded064393c20655a95ebc8441f5ad6e63ac6e Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 31 Oct 2024 17:10:46 +0530 Subject: [PATCH 2/2] Update clear.md minor fixes --- content/cpp/concepts/deque/terms/clear/clear.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/content/cpp/concepts/deque/terms/clear/clear.md b/content/cpp/concepts/deque/terms/clear/clear.md index 5af50f25803..23adf1d4414 100644 --- a/content/cpp/concepts/deque/terms/clear/clear.md +++ b/content/cpp/concepts/deque/terms/clear/clear.md @@ -22,8 +22,8 @@ In C++, the **`.clear()`** [method](https://www.codecademy.com/resources/docs/cp dequeName.clear(); ``` -- Has no parameters passed. - +- `dequeName`: The name of the deque container from which all elements will be removed. + ## Example The example below showcases the use of the `.clear()` method: @@ -42,17 +42,19 @@ int main() { numbers.push_back(150); numbers.push_back(200); - // Display the elements of the deque + // Display the elements of the deque before clearing std::cout << "My deque contains: "; for (int num : numbers) { std::cout << num << " "; } + // Clear all elements from the deque numbers.clear(); std::cout << std::endl; numbers.push_back(200); + // Display the elements of the deque after clearing and adding a new element std::cout << "My deque contains: "; for (int num : numbers) { std::cout << num << " "; @@ -73,8 +75,7 @@ My deque contains: 200 ## Codebyte Example -The following codebyte removes all values prior to calling the `.clear()` method. -Run the following example to understand the use of the `.clear()` method: +The following codebyte demonstrates the use of the `.clear()` method, which removes all elements from the deque before adding a new element: ```codebyte/cpp #include