From fa6afea8c0982393d0812654e86b51f1aa29397e Mon Sep 17 00:00:00 2001 From: smishra <8684346+HackersSpirit@users.noreply.github.com> Date: Mon, 13 Oct 2025 11:19:54 +1100 Subject: [PATCH 1/2] docs(cpp/deque): add .back() term entry with syntax, example, and Codebyte\n\n- New term: deque::back() at content/cpp/concepts/deque/terms/back/back.md\n- Includes description, Syntax, Example with output, and Codebyte example\n- Follows Docs term template, content standards, and style guide\n- Notes undefined behavior when called on empty deque --- content/cpp/concepts/deque/terms/back/back.md | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 content/cpp/concepts/deque/terms/back/back.md diff --git a/content/cpp/concepts/deque/terms/back/back.md b/content/cpp/concepts/deque/terms/back/back.md new file mode 100644 index 00000000000..7f8a9a3b5b4 --- /dev/null +++ b/content/cpp/concepts/deque/terms/back/back.md @@ -0,0 +1,77 @@ +--- +Title: '.back()' +Description: 'Accesses the last element in a deque without removing it.' +Subjects: + - 'Computer Science' + - 'Game Development' +Tags: + - 'Containers' + - 'Deques' + - 'Methods' + - 'Data Structures' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +In C++, the **`.back()`** [method](https://www.codecademy.com/resources/docs/cpp/methods) returns a reference to the last element of a deque. It can be used to read or modify that element without removing it from the container. + +> **Note:** Calling `.back()` on an empty deque results in undefined behavior. Check `.empty()` first if the deque might be empty. + +## Syntax + +```pseudo +dequeName.back(); +``` + +- Returns: A reference to the last element (`T&`); if the deque is `const`, returns `const T&`. +- Complexity: Constant time. +- Precondition: `dequeName` must not be empty. + +## Example + +The following example accesses the last element in a deque and then modifies it via the reference returned by `.back()`: + +```cpp +#include +#include + +int main() { + std::deque values = {10, 20, 30}; + + std::cout << "Back: " << values.back() << std::endl; + + // Modify the last element through the reference returned by .back() + values.back() = 99; + std::cout << "Back after modification: " << values.back() << std::endl; + + return 0; +} +``` + +This program produces the following output: + +```shell +Back: 30 +Back after modification: 99 +``` + +## Codebyte Example + +This Codebyte demonstrates reading and modifying the last element of a deque using `.back()`: + +```codebyte/cpp +#include +#include + +int main() { + std::deque words = {"alpha", "beta", "gamma"}; + + std::cout << words.back() << std::endl; // Prints the last element + + words.back() = "omega"; // Modify the last element + std::cout << words.back() << std::endl; // Prints the modified value + + return 0; +} +``` From c521dfe8964668631716ded88d9ad6951c6ac7b5 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 13 Oct 2025 16:26:50 +0530 Subject: [PATCH 2/2] minor fixes --- content/cpp/concepts/deque/terms/back/back.md | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/content/cpp/concepts/deque/terms/back/back.md b/content/cpp/concepts/deque/terms/back/back.md index 7f8a9a3b5b4..24feb595ae1 100644 --- a/content/cpp/concepts/deque/terms/back/back.md +++ b/content/cpp/concepts/deque/terms/back/back.md @@ -1,14 +1,14 @@ --- Title: '.back()' -Description: 'Accesses the last element in a deque without removing it.' +Description: 'Returns a reference to the last element in the deque.' Subjects: - 'Computer Science' - 'Game Development' Tags: - 'Containers' + - 'Data Structures' - 'Deques' - 'Methods' - - 'Data Structures' CatalogContent: - 'learn-c-plus-plus' - 'paths/computer-science' @@ -16,7 +16,7 @@ CatalogContent: In C++, the **`.back()`** [method](https://www.codecademy.com/resources/docs/cpp/methods) returns a reference to the last element of a deque. It can be used to read or modify that element without removing it from the container. -> **Note:** Calling `.back()` on an empty deque results in undefined behavior. Check `.empty()` first if the deque might be empty. +> **Note:** Calling `.back()` on an empty deque results in undefined behavior. Use `.empty()` to check before accessing elements. ## Syntax @@ -24,9 +24,13 @@ In C++, the **`.back()`** [method](https://www.codecademy.com/resources/docs/cpp dequeName.back(); ``` -- Returns: A reference to the last element (`T&`); if the deque is `const`, returns `const T&`. -- Complexity: Constant time. -- Precondition: `dequeName` must not be empty. +**Parameters:** + +This method does not take any parameters. + +**Return value:** + +Returns a reference to the last element of the deque. If the deque is `const`, it returns a const reference. ## Example @@ -58,7 +62,7 @@ Back after modification: 99 ## Codebyte Example -This Codebyte demonstrates reading and modifying the last element of a deque using `.back()`: +This example demonstrates reading and modifying the last element of a deque using `.back()`: ```codebyte/cpp #include