SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. #include <memory>
  5. #include <algorithm>
  6.  
  7.  
  8. template <typename T>
  9. class DiyVector
  10. {
  11. private:
  12.     unsigned int m_size = 0;
  13.     unsigned int m_capacity = 10;
  14.     std::unique_ptr<T[]> m_data = nullptr;
  15.  
  16.     void reserve(unsigned int size)
  17.     {
  18.         if (size < m_capacity)
  19.             return; //Don't shrink
  20.         else
  21.         {
  22.             std::unique_ptr<T[]> newData = std::make_unique<T[]>(size);
  23.             for (unsigned int i = 0; i < m_size; ++i)
  24.                 newData[i] = m_data[i];
  25.             m_data = std::move(newData);
  26.         }
  27.     }
  28.    
  29. public:
  30.     DiyVector() { m_data = std::make_unique<T[]>(10); }
  31.     ~DiyVector() = default;
  32.  
  33.     T& at(unsigned int index) const
  34.     {
  35.         if (index >= m_size)
  36.             throw new OutOfRange();
  37.         return m_data[index];
  38.     }
  39.  
  40.     unsigned int size() const
  41.     {
  42.         return m_size;
  43.     }
  44.  
  45.     void pushBack(const T& item)
  46.     {
  47.         if (m_size + 1 > m_capacity)
  48.             reserve(m_capacity * 1.5);
  49.         m_data[m_size++] = item;
  50.     }
  51.  
  52.     void popBack()
  53.     {
  54.         --m_size;
  55.     }
  56.  
  57.     void erase(unsigned int index)
  58.     {
  59.         if (index >= m_size)
  60.             throw new OutOfRange();
  61.         for (unsigned int i = index; i < m_size - 1; ++i)
  62.         {
  63.             m_data[i] = m_data[i] + 1;
  64.         }
  65.     }
  66.  
  67.     void insert(unsigned int index, const T& item)
  68.     {
  69.         if (index >= m_size + 1)
  70.             throw new OutOfRange();
  71.         if (m_size + 1 > m_capacity)
  72.             reserve(m_capacity * 1.5);
  73.         for (unsigned int i = index + 1; i < ++m_size; ++i)
  74.         {
  75.             m_data[i + 1] = m_data[i];
  76.         }
  77.         m_data[index] = item;
  78.     }
  79.  
  80.     void print()
  81.     {
  82.         for (int i = 0; i < m_size; i++)
  83.         {
  84.             std::cout << m_data[i] << " ";
  85.         }
  86.     }
  87.  
  88.     class OutOfRange {};
  89. };
  90.  
  91.  
  92. int main()
  93. {
  94.     DiyVector<int> v;
  95.     std::cout << "array size: " << v.size() << "\n";
  96.     v.pushBack(3);
  97.     std::cout << "3\n";  
  98.     std::cout << "array size: " << v.size() << "\n";
  99.     v.pushBack(4);
  100.     std::cout << "4\n";
  101.     v.pushBack(6);
  102.     std::cout << "6\n";
  103.     v.pushBack(2);
  104.     std::cout << "2\n";
  105.     std::cout << "array size: " << v.size() << "\n";
  106.     v.print();
  107.     std::cout << "\n";
  108.     v.insert(1, 7);
  109.     v.print();
  110.     std::cout << "\n";
  111.     v.erase(1);
  112.     v.print();
  113.     std::cout << "\n";
  114.     v.pushBack(5);
  115.     v.print();
  116.     std::cout << "\n";   
  117. }
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