Answers for "get last element of stack c++"

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
-1

get last element of stack c++

// stack::top
#include <iostream>       // std::cout
#include <stack>          // std::stack

int main ()
{
  std::stack<int> mystack;

  mystack.push(10);
  mystack.push(20);
  // .top() gets the last/top element of stack c++
  mystack.top() -= 5;

  std::cout << "mystack.top() is now " << mystack.top() << '\n';

  return 0;
}
Posted by: Guest on October-10-2020

Code answers related to "get last element of stack c++"

Browse Popular Code Answers by Language