back(): It returns the read and write reference to the data at the last element of the vector.
Syntax:
vecName.back()
It is available in the below file
File: stl_vector.h
For Example:
vec = {1,2,3,4}
vec.back() => It return 4.
Approach
C++
#include <bits/stdc++.h>using namespace std;int main(){vector<int> vec = {1, 2, 3, 4};//read the last element of the vectorcout << vec.back() << "\n";//update or write to the last element of//the vectorvec.back() = 10;cout << vec.back() << "\n";return 0;}
No comments:
Post a Comment