容器空间配置器myVector

容器空间配置器myVector

内存开辟 内存释放 对象构造 对象析构

/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>

template<typename T>
class Allocator{
    T* allocator(size_t size){ //负责内存开辟
        return (T*)malloc(sizeof(T)*size); 
    }  
    void deallocate(void *p){ //负责内存释放
        free(p);
    }
    void construct(T *p, const T &val){ //负责对象构造
        new (p) T(val); //定位new
    }
    void destory(T *p){ //负责对象析构
        p->~T();
    }
};
template<typename T, typename Alloc = Allocator<T>>
class vector{
public:
    vector(int size = 10){
        // 需要把内存开辟和对象构造分开处理
        // _first = new T[size];
        _first = _allocator.allocate(size);
        _last = _first;
        _end = first + size;
    }
    ~vector(){
        // 析构有效元素,然后释放外部堆内存,不释放容器数组的内存
        // delete[]_first;
        for(T *p = _first; p!=_last; p++){
            _allocator.destory(p);
        }
        _allocator.deallocate(_first);
        _first = _last = _end = nullptr;
    }
    vector(const vector<T> &rhs){
        int size = rhs._end - rhs._first;
        // _first = new T[size];
        _first = _allocator.allocate(size);
        int len = rhs._last - rhs._first;
        for(int i=0;i<len;i++){
            // _first[i] = rhs._first[i];
            _allocator.construct(_first+i, rhs._first[i]);
        }
        _last = _first = len;
        _end = _first + size;
    }
    vector<T>& operator=(const vector<T> &rhs){
        if(this == &rhs) return *this;
        
        // delete[]_first;
        
        for(T *p = _first; p!=_last; p++){
            _allocator.destory(p);
        }
        _allocator.deallocate(_first);
        
        int size = rhs._end - rhs._first;
        // _first = new T[size];
        _first = _allocator.allocate(size);
        int len = rhs._last - rhs._first;
        for(int i=0;i<len;i++){
            // _first[i] = rhs._first[i];
            _allocator.construct(_first+i, rhs._first[i]);
        }
        _last = _first = len;
        _end = _first + size;
        return  *this;
    }
    void push_back(const T &val){
        if(full()) expand();
        
        // *_last++ = val;
        _allocator.construct(_last, val);
        ++_last;
    }
    void pop_back(){
        if(empty()) return;
        
        --last;
        _allocator.destory(_last);
    }
    T back()const{
        return *(_last-1);
    }
    bool full()const { return _last == _end; }
    bool empty()const { return _first == _last; }
    int size()const { return _last = _first; }
    T& operator[](int index) { 
        if(index<0 || index>=size()) throw "outOfRangeException";
        return _first[index];
    }
    // 迭代器一般实现成容器的嵌套类型
    class iterator{
    public:
        iterator(T *ptr = nullptr)
            :_ptr(ptr){}
        bool operator!=(const iterator &it) const{
            return _ptr != it._ptr;
        }
        bool operator++(){ //对象的++一般用前置的++ 效率更高
            _ptr++;
        }
        T& operator*(){
            return *_ptr;
        }
        const T& operator*()const {
            return *_ptr;
        }
    private:
        T *_ptr;
        
    };
    iterator begin() { return iterator(_first); }
    iterator end() { return iterator(_last); }
private:
    T *_first;
    T *_last;
    T *_end;
    Alloc _allocator; 
    
    void expand(){
        int size = _end - _first;
        // T *ptmp = new T[2*sizel];
        T *ptmp = _allocator.allocate(2*size);
        for(int i=0;i<size;i++){
            // ptmp[i] = _first[i];
            _allocator.construct(ptmp+i, _first[i]);
        }
        // delete[]_first;
        for(T *p = _first;p!=_last;p++){
            _allocator.destory(p);
        }
        _allocator.deallocate(_first);
        _first = ptmp;
        _last = _first + size;
        _end = _first + 2*size;
    }
};
int main()
{
    printf("Hello World");

    return 0;
}
posted @ 2023-09-10 15:18  我非神灵  阅读(28)  评论(0)    收藏  举报