SHARE
TWEET

Untitled

a guest Sep 22nd, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template<class T>
  6. class my_vector {
  7.  public:
  8.   my_vector(const size_t new_capacity = 100) {
  9.     capacity = new_capacity;
  10.     arr_size = 0;
  11.     m = new T[new_capacity];
  12.   }
  13.  
  14.   void push_back(const T x) {
  15.     if (arr_size < capacity) {
  16.       m[arr_size] = x;
  17.       arr_size++;
  18.     } else {
  19.       resize(capacity * (size_t) 2);
  20.       m[arr_size] = x;
  21.       arr_size++;
  22.     }
  23.   }
  24.  
  25.   int pop_back() {
  26.     if (arr_size <= capacity / 4) {
  27.       resize(capacity / (size_t) 2);
  28.     }
  29.     arr_size--;
  30.     return m[arr_size];
  31.   }
  32.  
  33.   size_t size() const {
  34.     return arr_size;
  35.   }
  36.  
  37.   T &operator[](const int index) {
  38.     return m[index];
  39.   }
  40.  
  41.  private:
  42.   T *m;
  43.   size_t arr_size;
  44.   size_t capacity;
  45.  
  46.   void resize(size_t new_capacity) {
  47.     T *p = m;
  48.     capacity = new_capacity;
  49.     m = new T[capacity];
  50.     for (size_t i = 0; i < arr_size; ++i) {
  51.       m[i] = p[i];
  52.     }
  53.     delete[] p;
  54.   }
  55. };
  56.  
  57. template<class T>
  58. class my_stack {
  59.  public:
  60.   bool empty() const {
  61.     return s.size() == 0;
  62.   }
  63.  
  64.   void push(T x) {
  65.     s.push_back(x);
  66.   }
  67.  
  68.   T pop() {
  69.     return s.pop_back();
  70.   }
  71.  
  72.  private:
  73.   my_vector<T> s = my_vector<T>(10);
  74. };
  75.  
  76. int main() {
  77.   my_stack<int> stack;
  78.   int M;
  79.   cin >> M;
  80.   for (int i = 0; i < M; ++i) {
  81.     char action;
  82.     cin >> action;
  83.     if (action == '-') {
  84.       cout << stack.pop() << endl;
  85.     } else if (action == '+') {
  86.       int N;
  87.       cin >> N;
  88.       stack.push(N);
  89.     }
  90.   }
  91.   return 0;
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top