How to remove elements from the top of stack in C++

1 Answer

0 votes
#include <iostream>       
#include <stack>          

using namespace std; 

void printStack(stack<int> st) {
  while (!st.empty()) {
     cout << st.top() << ' ';
     st.pop();
  }
}

int main ()
{
    stack<int> st;

    for (int i = 0; i < 5; i++) 
        st.push(i);

    printStack(st);
    
    st.pop();
    st.pop();

    cout << endl;
    
    printStack(st);

    return 0;
} 
   
   
   
/*
run:
   
4 3 2 1 0 
2 1 0 
   
*/

 



answered Apr 6, 2020 by avibootz

Related questions

...