From bc1963023fa06eda3d04870e1a8b4dd55b5dd0c1 Mon Sep 17 00:00:00 2001 From: Fejiro001 Date: Wed, 30 Oct 2024 16:45:59 +0100 Subject: [PATCH 1/3] [Term Entry] C++ Deque Empty() --- .../cpp/concepts/deque/terms/empty/empty.md | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 content/cpp/concepts/deque/terms/empty/empty.md diff --git a/content/cpp/concepts/deque/terms/empty/empty.md b/content/cpp/concepts/deque/terms/empty/empty.md new file mode 100644 index 00000000000..a5607d00932 --- /dev/null +++ b/content/cpp/concepts/deque/terms/empty/empty.md @@ -0,0 +1,91 @@ +--- +Title: '.empty()' +Description: 'Checks if the Deque container is empty or not.' +Subjects: + - 'Computer Science' + - 'Game Development' +Tags: + - 'Classes' + - 'Containers' + - 'Deques' + - 'OOP' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +In C++, the **`.empty()`** [method](https://www.codecademy.com/resources/docs/cpp/methods) checks if the deque container is empty (size is 0) or not. + +## Syntax + +```pseudo +dequeName.empty(); +``` + +- Has no parameters passed. +- Returns `true` if empty and returns `false` if not empty. + +## Example + +The example below showcases the use of the `.empty()` method: + +```cpp +#include +#include + +int main() { + // Create a deque of integers + std::deque numbers; + + // Add elements to the deque + numbers.push_back(100); + numbers.push_back(150); + + std::cout << "Deque is empty: "; + if (numbers.empty()) { + std::cout << "True"; + } + else { + std::cout << "False"; + } + + std::cout << std::endl; + + return 0; +} +``` + +The above code generates the following output: + +```shell +Deque is empty: False +``` + +## 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("Car"); + myDeque.push_back("Bus"); + myDeque.push_back("Train"); + + if (myDeque.empty()) { + std::cout << "Deque is empty"; + } + else { + std::cout << "Vehicles:"; + for (const auto& value : myDeque) { + std::cout << ' ' << value; + } + } +} +``` From 1827c603bcedf9acf4c60a9756d31ef715320c49 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 31 Oct 2024 16:49:06 +0530 Subject: [PATCH 2/3] Update empty.md minor fixes --- .../cpp/concepts/deque/terms/empty/empty.md | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/content/cpp/concepts/deque/terms/empty/empty.md b/content/cpp/concepts/deque/terms/empty/empty.md index a5607d00932..22a8f889e0c 100644 --- a/content/cpp/concepts/deque/terms/empty/empty.md +++ b/content/cpp/concepts/deque/terms/empty/empty.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/computer-science' --- -In C++, the **`.empty()`** [method](https://www.codecademy.com/resources/docs/cpp/methods) checks if the deque container is empty (size is 0) or not. +In C++, the **`.empty()`** [method](https://www.codecademy.com/resources/docs/cpp/methods) checks if the deque container is empty (i.e., if its size is 0) and returns `true` if it is empty or `false` otherwise. ## Syntax @@ -22,8 +22,7 @@ In C++, the **`.empty()`** [method](https://www.codecademy.com/resources/docs/cp dequeName.empty(); ``` -- Has no parameters passed. -- Returns `true` if empty and returns `false` if not empty. +- `dequeName`: The variable name of the deque being checked for emptiness. ## Example @@ -63,8 +62,7 @@ Deque is empty: False ## 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 uses the `.clear()` method on a deque and checks its emptiness by outputting a boolean value using the `.empty()` method: ```codebyte/cpp #include @@ -78,14 +76,19 @@ int main() { myDeque.push_back("Bus"); myDeque.push_back("Train"); - if (myDeque.empty()) { - std::cout << "Deque is empty"; - } - else { - std::cout << "Vehicles:"; - for (const auto& value : myDeque) { - std::cout << ' ' << value; - } + // Displaying vehicles before clearing the deque + std::cout << "Vehicles before clearing:"; + for (const auto& value : myDeque) { + std::cout << ' ' << value; } + std::cout << std::endl; + + // Clear all elements from the deque + myDeque.clear(); + + // Boolean output indicating if the deque is empty after clearing + std::cout << "Deque is empty after clearing: " << std::boolalpha << myDeque.empty() << std::endl; + + return 0; } ``` From 278a7b180a9da8fe73be3a68ad59b8b6588f82ca Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 31 Oct 2024 16:53:13 +0530 Subject: [PATCH 3/3] Update empty.md --- content/cpp/concepts/deque/terms/empty/empty.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/deque/terms/empty/empty.md b/content/cpp/concepts/deque/terms/empty/empty.md index 22a8f889e0c..0f5fc70f659 100644 --- a/content/cpp/concepts/deque/terms/empty/empty.md +++ b/content/cpp/concepts/deque/terms/empty/empty.md @@ -1,6 +1,6 @@ --- Title: '.empty()' -Description: 'Checks if the Deque container is empty or not.' +Description: 'Returns true if the deque container is empty and false if it contains elements.' Subjects: - 'Computer Science' - 'Game Development'