Cypher121

Untitled

Oct 2nd, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T>
  6. struct my_vector{
  7.     int length;
  8.     int tail;
  9.     T* data;
  10.     my_vector() {
  11.         length = 10;
  12.         tail = 0;
  13.         data = (T*) malloc(length * sizeof(T));
  14.     }
  15.     void expand() {
  16.         if (tail == length) {
  17.             length += length;
  18.             data = (T*) realloc(data, length * sizeof(T));
  19.         }
  20.     }
  21.  
  22.     void push(T x) {
  23.         data[tail] = x;
  24.         tail++;
  25.         expand();
  26.     }
  27.  
  28.     T pop() {
  29.         tail--;
  30.         return data[tail];
  31.     }
  32. };
  33.  
  34.  
  35. int main() {
  36.     my_vector<int> a;
  37.     a.push(1);
  38.     a.push(4);
  39.     cout << a.pop() << endl;
  40.     cout << a.pop() << endl;
  41.    
  42.     my_vector<double> b;
  43.     b.push(3.5);
  44.     b.push(6.6);
  45.     cout << b.pop() << endl;
  46.     cout << b.pop() << endl;
  47.    
  48.     my_vector<string> c;
  49.     c.push("aaa");
  50.     c.push("bbb");
  51.     cout << c.pop() << endl;
  52.     cout << c.pop() << endl;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment