fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. vector<int> v;
  9.  
  10. int main() {
  11. ios_base::sync_with_stdio(false);
  12. cin.tie(NULL);
  13.  
  14. vector<int> v;
  15.  
  16. int N;
  17. cin >> N;
  18. string input;
  19. getchar();
  20. for (int i = 0; i < N; i++) {
  21. getline(cin, input);
  22. string command = input.substr(0, input.find(' '));
  23. if (command == "push") {
  24. v.push_back(stoi((input.substr(input.find(' ') + 1, -1))));
  25. }
  26. else if (command == "pop") {
  27. if (v.size() == 0) {
  28. cout << "-1" << '\n';
  29. }
  30. else {
  31. cout << v[v.size()-1] << '\n';
  32. v.pop_back();
  33. }
  34. }
  35. else if (command == "size") {
  36. cout << v.size() << '\n';
  37. }
  38. else if (command == "empty") {
  39. if (v.size() == 0)
  40. cout << "1" << '\n';
  41. else
  42. cout << "0" << '\n';
  43. }
  44. else {
  45. if (v.size() == 0)
  46. cout << "-1" << '\n';
  47. else
  48. cout << v[v.size()-1] << '\n';
  49. }
  50. }
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0s 4388KB
stdin
7
pop
top
push 123
top
pop
top
pop
stdout
-1
-1
-1
123
123
-1