how to access last element of vector code example
Example 1: 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 2: c++ access second last element of vector
arr2.rbegin()[1] // rbegin() is reverse order starting at 0 for last element, 1 for second-last
Example 3: c++ get last element in vector
std::v.back()