Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <class T>
- inline void my_vector<T>::reallocate() {
- T* tarray = reinterpret_cast<T*>(new int8_t[capacity_ * sizeof(T)]);
- for (size_t i = 0; i < size_; i++)
- new (&tarray[i]) T(array_[i]);
- delete[] reinterpret_cast<int8_t*>(array_);
- array_ = tarray;
- }
- template <class T>
- void my_vector<T>::resize(size_t size) {
- reserve(size);
- for (size_t i = size; i < size_; i++)
- array_[i].~T();
- for (size_t i = size_; i < size; i++)
- new (&array_[i]) T();
- size_ = size;
- }
- template <class T>
- void my_vector<T>::reserve(size_t capacity) {
- if (capacity > capacity_) {
- capacity_ = 1;
- while (capacity_ < capacity)
- capacity_ <<= 1;
- reallocate();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement