Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- template <class T>
- class myVector{
- private:
- T numItems, maxSize;
- T* items;
- void resize(T newCap)
- {
- //step 1: create a new bigger array
- T* tmp = new int[newCap];
- // step 2: copy contents of old array to the new bigger array
- for (int i = 0; i < maxSize; i++)
- {
- tmp[i] = items[i];
- }
- //step 3: delete old array
- delete[] items;
- //step 4: poT'items' array variable to the new array
- items = tmp;
- // step 5: update capacity
- maxSize = newCap;
- }
- public:
- myVector()
- {
- maxSize = 20;
- items = new int[maxSize];
- numItems = 0;
- }
- myVector(T capacity)
- {
- maxSize = capacity;
- items = new int[maxSize];
- numItems = 0;
- }
- T size()
- {
- return numItems;
- }
- T capacity()
- {
- return maxSize;
- }
- void push_back(T x)
- {
- if (numItems == maxSize)
- {
- resize(maxSize*2);
- }
- items[numItems] = x;
- numItems++;
- }
- void back()
- {
- numItems--;
- return items[numItems];
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement