Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. template <class T>
  2. inline void my_vector<T>::reallocate() {
  3.     T* tarray = reinterpret_cast<T*>(new int8_t[capacity_ * sizeof(T)]);
  4.     for (size_t i = 0; i < size_; i++)
  5.         new (&tarray[i]) T(array_[i]);
  6.     delete[] reinterpret_cast<int8_t*>(array_);
  7.     array_ = tarray;
  8. }
  9.  
  10. template <class T>
  11. void my_vector<T>::resize(size_t size) {
  12.     reserve(size);
  13.     for (size_t i = size; i < size_; i++)
  14.         array_[i].~T();
  15.     for (size_t i = size_; i < size; i++)
  16.         new (&array_[i]) T();
  17.     size_ = size;
  18. }
  19.  
  20. template <class T>
  21. void my_vector<T>::reserve(size_t capacity) {
  22.     if (capacity > capacity_) {
  23.         capacity_ = 1;
  24.         while (capacity_ < capacity)
  25.             capacity_ <<= 1;
  26.         reallocate();
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement