Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions content/cpp/concepts/queues/terms/back/back.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
---
Title: '.back()'
Description: 'Returns a reference to the element at the back of the queue.'
Subjects:
- 'Computer Science'
- 'Game Development'
Tags:
- 'Classes'
- 'Methods'
- 'Objects'
- 'OOP'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

The C++ **`.back()`** [method](https://www.codecademy.com/resources/docs/cpp/methods) is used to access the element at the back of the queue without removing it.

## C++ `.back()` Syntax

```pseudo
queueName.back();
```

**Parameters:**

The C++ `.back()` method takes no parameters.

**Return value:**

Returns a reference to the element at the end of the queue.

## Example 1: Basic usage of C++ `.back()`

This example uses C++ `.back()` to access the last element of the queue:

```cpp
#include <iostream>
#include <queue>
using namespace std;

int main() {
// Declaring a queue
queue<int> q;

// Inserting int elements into the queue
q.push(10);
q.push(20);
q.push(30);

// Print the last element of the queue
cout <<"Last element: " << q.back() << endl;
}
```

Here is the output of this code:

```shell
Last element: 30
```

## Example 2: Using C++ `.back()` with `.push()`

This example uses C++ `.back()` to return the last element of the queue after we used `.push()`:

```cpp
#include <iostream>
#include <queue>
using namespace std;

int main () {
queue<string> q;

q.push("Jhon");
q.push("Bob");
q.push("Doe");

cout <<"Last element: " << q.back() << endl;
q.push("Alice");
cout <<"Last element after push: " << q.back() << endl;
}
```

Here is the output of this code:

```shell
Last element: Doe
Last element after push: Alice
```

## Codebyte Example: Modifying the Last Element Using C++ `.back()`

This codebyte example uses C++ `.back()` to modify the last element of the queue:

```codebyte/cpp
#include <iostream>
#include <queue>
using namespace std;

int main() {
queue<int> q;

q.push(10);
q.push(20);

// Print the last element
cout << "Last element before modification: " << q.back() << endl;

// Modify the last element
q.back() = 100;

// Print the modified last element
cout << "Last element after modification: " << q.back() << endl;

return 0;
}
```
Loading