c++ find last element in vector code example

Example 1: find last element of an array c++

int arr={1,2,3,4,5,6};
int length=sizeof(arr)/sizeof(int);
int lastElement=aar[length-1];

Example 2: access last element in vector in c++

vector<int> v;
cout << v[v.size() - 1];
cout << *(v.end() - 1);
cout << *v.rbegin();
// all three of them work

Example 3: cpp get last element of vector

vector<int> vec;
vec.push_back(0);
vec.push_back(1);
int last_element = vec.back();
int also_last_element = vec[vec.size() - 1];

Example 4: c++ get last element in vector

std::v.back()

Tags:

Cpp Example