Answers for "c++ access second last element of vector"

C++
18

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
Posted by: Guest on May-31-2020
2

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];
Posted by: Guest on September-05-2020
0

c++ access second last element of vector

arr2.rbegin()[1] // rbegin() is reverse order starting at 0 for last element, 1 for second-last
Posted by: Guest on May-07-2021
1

c++ get last element in vector

std::v.back()
Posted by: Guest on May-30-2020

Code answers related to "c++ access second last element of vector"

Browse Popular Code Answers by Language