c++ get last element in vector code example
Example 1: 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 2: c++ vector.back
#include <iostream>
#include <vector>
int main()
{
std::vector<int> myvector;
myvector.push_back(2);
std::cout << myvector.back() << std::endl;
myvector.push_back(46);
std::cout << myvector.back() << std::endl;
return 0;
}
Example 3: c++ get last element in vector
std::v.back()