stack container in C++

Level 4214 keystrokes

A stack is last in, first out: the most recently pushed value is on top, so after one pop the top changes from 30 to 20.

C++ code

1stack<int> s;
// push places each value on top of the stack
2s.push(10);
3s.push(20);
4s.push(30);
// top is the last value pushed: prints Top: 30
5cout << "Top: " << s.top() << endl;
// pop removes the top element (30) and returns nothing
6s.pop();
// Prints After pop: 20
7cout << "After pop: " << s.top() << endl;
// Two elements are left: prints Size: 2
8cout << "Size: " << s.size() << endl;
// empty() is false, printed as 0: Empty: 0
9cout << "Empty: " << s.empty() << endl;

Output

Top: 30
After pop: 20
Size: 2
Empty: 0
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 25
  • ;× 9
  • .× 8
  • "× 8
  • (× 8
  • )× 8
  • :× 4
  • >× 1

More C++ examples at level 4

← C++ typing practice