Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. template <class T>
  7. class myVector{
  8. private:
  9.     T numItems, maxSize;
  10.     T* items;
  11.     void resize(T newCap)
  12.     {
  13.         //step 1: create a new bigger array
  14.         T* tmp = new int[newCap];
  15.        
  16. //        step 2: copy contents of old array to the new bigger array
  17.         for (int i = 0; i < maxSize; i++)
  18.         {
  19.             tmp[i] = items[i];
  20.         }
  21.        
  22.         //step 3: delete old array
  23.         delete[] items;
  24.        
  25.         //step 4: poT'items' array variable to the new array
  26.         items = tmp;
  27.        
  28. //        step 5: update capacity
  29.         maxSize = newCap;
  30.        
  31.     }
  32.    
  33. public:
  34.     myVector()
  35.     {
  36.         maxSize = 20;
  37.         items = new int[maxSize];
  38.         numItems = 0;
  39.     }
  40.     myVector(T capacity)
  41.     {
  42.         maxSize = capacity;
  43.         items = new int[maxSize];
  44.         numItems = 0;
  45.     }
  46.    
  47.     T size()
  48.     {
  49.         return numItems;
  50.     }
  51.    
  52.     T capacity()
  53.     {
  54.         return maxSize;
  55.     }
  56.    
  57.     void push_back(T x)
  58.     {
  59.         if (numItems == maxSize)
  60.         {
  61.             resize(maxSize*2);
  62.         }
  63.         items[numItems] = x;
  64.         numItems++;
  65.     }
  66.    
  67.     void back()
  68.     {
  69.         numItems--;
  70.         return items[numItems];
  71.     }
  72.    
  73.    
  74. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement