1vector<int> v;
2cout << v[v.size() - 1];
3cout << *(v.end() - 1);
4cout << *v.rbegin();
5// all three of them work
1#include<iostream>
2/*To get the last element of the array we first get the size
3 of the array by using sizeof(). Unfortunately, this gives
4 us the size of the array in bytes. To fix this, we divide
5 the size (in bytes) by the size of the data type in the array.
6 In our case, this would be int, so we divide sizeof(array)
7 by sizeof(int). Since arrays start from 0 and not 1 we
8 subtract one to get the last element.
9 -yegor*/
10int array[5] = { 1, 2, 3, 4, 5 };
11printf("Last Element of Array: %d", array[(sizeof(array)/sizeof(int))-1]);
1vector<int> vec;
2vec.push_back(0);
3vec.push_back(1);
4int last_element = vec.back();
5int also_last_element = vec[vec.size() - 1];
6