|
| 1 | +--- |
| 2 | +Title: '.front()' |
| 3 | +Description: 'Returns a reference to the first element of the deque' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Game Development' |
| 7 | +Tags: |
| 8 | + - 'Classes' |
| 9 | + - 'Containers' |
| 10 | + - 'Deques' |
| 11 | + - 'OOP' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-c-plus-plus' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +In C++, the **`.front()`** [method](https://www.codecademy.com/resources/docs/cpp/methods) returns a reference to the first element in the [deque](https://www.codecademy.com/resources/docs/python/deque). |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +deque_name.front() |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +`.front()` does not take any parameters |
| 28 | + |
| 29 | +**Return value:** |
| 30 | + |
| 31 | +- Returns a reference to the first element of the deque. |
| 32 | + - For a non-const deque, returns `T&` (modifiable). |
| 33 | + - For a const deque, returns const `T&` (read-only). |
| 34 | + |
| 35 | +## Example |
| 36 | + |
| 37 | +The example below demonstrates use of `.front()` to access the first element in a deque: |
| 38 | + |
| 39 | +```cpp |
| 40 | +#include <iostream> |
| 41 | +#include <deque> |
| 42 | + |
| 43 | +int main() { |
| 44 | + // Create a deque of integers |
| 45 | + std::deque<int> numbers; |
| 46 | + |
| 47 | + // Add some elements to the deque |
| 48 | + numbers.push_back(100); |
| 49 | + numbers.push_back(200); |
| 50 | + numbers.push_back(300); |
| 51 | + |
| 52 | + // Access the first element using .front() |
| 53 | + std::cout << "First element: " << numbers.front() << std::endl; |
| 54 | + |
| 55 | + // Modify the first element |
| 56 | + numbers.front() = 50; |
| 57 | + |
| 58 | + // Display updated deque contents |
| 59 | + std::cout << "Updated deque: "; |
| 60 | + for (int num : numbers) { |
| 61 | + std::cout << num << " "; |
| 62 | + } |
| 63 | + std::cout << std::endl; |
| 64 | + |
| 65 | + return 0; |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +The output of this program will be: |
| 70 | + |
| 71 | +```shell |
| 72 | +First element: 100 |
| 73 | +Updated deque: 50 200 300 |
| 74 | +``` |
| 75 | + |
| 76 | +This shows that `.front()` allows both access and modification of the deque’s first element. |
| 77 | + |
| 78 | +## Codebyte Example |
| 79 | + |
| 80 | +Run the following codebyte example to understand the use of `.front()` method: |
| 81 | + |
| 82 | +```codebyte/cpp |
| 83 | +#include <iostream> |
| 84 | +#include <deque> |
| 85 | +#include <string> |
| 86 | +
|
| 87 | +int main() { |
| 88 | + // Create a deque of strings |
| 89 | + std::deque<std::string> myDeque; |
| 90 | +
|
| 91 | + // Add elements to the deque |
| 92 | + myDeque.push_back("Hello"); |
| 93 | + myDeque.push_back("World"); |
| 94 | + myDeque.push_back("!"); |
| 95 | +
|
| 96 | + // Print the front element |
| 97 | + std::cout << "Front element before change: " << myDeque.front() << std::endl; |
| 98 | +
|
| 99 | + // Modify the front element |
| 100 | + myDeque.front() = "Hi"; |
| 101 | +
|
| 102 | + // Print the modified front element |
| 103 | + std::cout << "Front element after change: " << myDeque.front() << std::endl; |
| 104 | +
|
| 105 | + // Print all elements of the deque |
| 106 | + std::cout << "Complete deque: "; |
| 107 | + for (const auto& str : myDeque) { |
| 108 | + std::cout << str << " "; |
| 109 | + } |
| 110 | + std::cout << std::endl; |
| 111 | +
|
| 112 | + return 0; |
| 113 | +} |
| 114 | +``` |
0 commit comments