From 00eb5173063747cad36cf54622d97dd0167f2a38 Mon Sep 17 00:00:00 2001 From: Savi Dahegaonkar <124272050+SaviDahegaonkar@users.noreply.github.com> Date: Fri, 18 Oct 2024 11:00:42 +0530 Subject: [PATCH] [Term Entry] C++ vector .back() (#5505) * New file has been added. * Update user-input.md * Update user-input.md * File has been modified. * Update content/cpp/concepts/vectors/terms/back/back.md * Errors Fixed --------- --- .../cpp/concepts/vectors/terms/back/back.md | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 content/cpp/concepts/vectors/terms/back/back.md diff --git a/content/cpp/concepts/vectors/terms/back/back.md b/content/cpp/concepts/vectors/terms/back/back.md new file mode 100644 index 00000000000..71e62e0dcb0 --- /dev/null +++ b/content/cpp/concepts/vectors/terms/back/back.md @@ -0,0 +1,64 @@ +--- +Title: '.back()' +Description: 'Used to access the last element in a vector.' +Subjects: + - 'Computer Science' + - 'Game Development' +Tags: + - 'Vectors' + - 'Programming' + - 'Data Structures' + - 'Methods' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The C++ **`.back()`** method is used to access the last element in a vector. It views or modifies the element without removing it from the vector. This method is primarily used to access the most recently added element. + +## Syntax + +```pseudo +vectorName.back(); +``` + +## Example + +The below example shows the use of `.back()` method in c++ vectors, here `numbers` is a vector which has 5 elements in it and it displays the last element in the vector with the help of the `.back()` method, then the last element in the vector is modifies and the modified value is displayed. + +```cpp +#include +#include +//This is compulsory to include vectors while using vectors. + +int main(){ + std::vectornumbers = {10,20,40,50,60}; + std::cout<<"The last element in the vector is: "<< numbers.back()<< std::endl; + numbers.back() = 80; + std::cout <<"The last element in the vector after modification is: "< +#include +int main() { + std::vector numbers = {100, 90, 80, 70, 60}; + std::cout << numbers.back() << std::endl; + numbers.back() = 10; + std::cout << numbers.back() << std::endl; + return 0; +} +```