Answers for "c++ get last element in vector"

C++
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
3

c++ vector.back

#include <iostream>
#include <vector>

int main()
{
  std::vector<int> myvector;
  
  //add 2 to the back
  myvector.push_back(2);
  
  std::cout << myvector.back() << std::endl; //this will print 2
  
  myvector.push_back(46);
  std::cout << myvector.back() << std::endl; //prints 46
  
  return 0;
  
}

/*Output
2
46
*/
Posted by: Guest on August-14-2020
1

c++ get last element in vector

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

Code answers related to "c++ get last element in vector"

Browse Popular Code Answers by Language