- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Last element of vector in C++ (Accessing and updating)
In this article, we will be discussing the ways of accessing and updating the last element of a vector in C++.
What is a vector template?
Vectors are sequence containers whose size is changed dynamically. A container is an object that holds data of the same type. Sequence containers store elements strictly in a linear sequence.
Vector container stores the elements in contiguous memory locations and enables direct access to any element using a subscript operator []. Unlike arrays, the vector’s size is dynamic. The storage of the vector is handled automatically.
Definition of vector
Template <class T, class Alloc = allocator<T>> class vector;
Parameters of vector
The function accepts the following parameter(s) −
T − This is the type of element contained.
Alloc − This is the type of allocator object.
How can we access the last element of vector?
To access the last element of the vector we can use two methods:
Example
using back() function
#include <bits/stdc++.h> using namespace std; int main(){ vector<int> vec = {11, 22, 33, 44, 55}; cout<<"Elements in the vector before updating: "; for(auto i = vec.begin(); i!= vec.end(); ++i){ cout << *i << " "; } // call back() for fetching last element cout<<"\nLast element in vector is: "<<vec.back(); vec.back() = 66; cout<<"\nElements in the vector before updating: "; for(auto i = vec.begin(); i!= vec.end(); ++i){ cout << *i << " "; } return 0; }
Output
If we run the above code it will generate the following output −
Elements in the vector before updating: 11 22 33 44 55 Last element in vector is: 55 Elements in the vector before updating: 11 22 33 44 66
Example
using size() function
#include <bits/stdc++.h> using namespace std; int main(){ vector<int> vec = {11, 22, 33, 44, 55}; cout<<"Elements in the vector before updating: "; for(auto i = vec.begin(); i!= vec.end(); ++i){ cout << *i << " "; } // call size() for fetching last element int last = vec.size(); cout<<"\nLast element in vector is: "<<vec[last-1]; vec[last-1] = 66; cout<<"\nElements in the vector before updating: "; for(auto i = vec.begin(); i!= vec.end(); ++i){ cout << *i <<" "; } return 0; }
Output
If we run the above code it will generate the following output −
Elements in the vector before updating: 11 22 33 44 55 Last element in vector is: 55 Elements in the vector before updating: 11 22 33 44 66
- Related Articles
- Updating last entry of a particular ID in MySQL
- Accessing inner element of JSON array in MongoDB?
- Get first and last elements from Vector in Java
- Accessing Attributes and Methods in C#
- Accessing a parent Element using JavaScript
- First element and last element in a JavaScript array?
- How to minus every element of a vector with every element of another vector in R?
- Accessing nth element from Python tuples in list
- Maximum difference between first and last indexes of an element in array in C
- Search an element of Vector in Java
- Finding the index of last element in the array in C#
- Accessing variables of Int and Float without initializing in C
- Accessing element which is defined SAPUI5 application
- Accessing array out of bounds in C/C++
- Find last element after deleting every second element in array of n integers in C++
