QT Day1

手动实现vector

#include 
#include 

using namespace std;

template  
class Myvector{
private:
    T * first;//头
    T * last;//尾
    T * end;//当前位置
public:
    Myvector(int size) {//无参构造d
        first = new T[size];
        end = first;
        last = first+size;
    }
    ~Myvector() {//析构函数
        delete []first;
        first = nullptr;//将指针都释放掉
        last = nullptr;
        end = nullptr;
    }
    Myvector(const Myvector &other){//拷贝构造
        int count = other.end - other.first;
        int size = other.last - other.first;
        this->first = new T[size];
        memcpy(this->first, other.first, sizeof(T)*count);
        this->end = this->first + count;
        this->end = this->first + size;
    }
    Myvector &operator=(const Myvector &other);
    T &at(int pos);
    bool empty();
    bool full();
    T &front();
    T &back();
    int size();
    void clear();
    void expand();
    void push_back(const T data);
    void pop_back();
    void show();
};
//拷贝赋值函数
template  
Myvector &Myvector::operator=(const Myvector &other){
    if(this != other){
        int count = other.end - other.first;
        int size = other.last - other.first;
        this->first = new T[size];
        memcpy(this->first, other.first, sizeof(T)*count);
        this->end = this->first + count;
        this->end = this->first + size;
    }
    return *this;
}
//容器判空
template  
bool Myvector::empty(){
    if(end-first)
        return false;
    else
        return true;//元素个数大小等于0就是空
}
//按位置访问
template  
T &Myvector::at(int pos){
    if(pos > end-first || pos < 0)//判断pos有没有越界
        throw std::out_of_range("error");
    return *(first+pos-1);//返回访问数据
}
//容器判满
template  
bool Myvector::full(){
    if(this->end == this->last)//当前位置等于尾就是满
        return true;
    else
        return false;
}
//访问第一个元素
template  
T &Myvector::front(){
    if(this->empty())//判空
        throw std::out_of_range("empty");
    return *first;//返回第一个元素
}
//访问最后一个元素
template  
T &Myvector::back(){
    if(this->empty())//判空
        throw std::out_of_range("empty");
    return *(end-1);//返回最后一个元素
}
//容器元素的大小
template  
int Myvector::size(){
    return end-first;//尾减头
}
//清空元素
template  
void Myvector::clear(){
    delete []this->first;
    this->first = nullptr;//将指针都释放掉
    this->last = nullptr;
    this->end = nullptr;
}
//二倍扩容
template  
void Myvector::expand(){
    int size = this->last - this->first;
    Myvector temp(size);
    memcpy(temp.first, this->first, sizeof(T)*size);
    delete []this->first;
    this->first = this->first;
    this->end = this->end;
    this->last = this->first+size*2;
}
//尾插
template  
void Myvector::push_back(const T data){
    if(full())//判满
        expand();//满了就二倍扩容
    *end = data;//赋值
    end+=1;//尾加1
}
//尾删
template  
void Myvector::pop_back(){
    if(empty())//判空
        throw std::out_of_range("empty");
    end-=1;//传完参尾减1
}
template  
void Myvector::show(){
    for(auto p=first; p!=end; p++)
        cout<<*p<<" ";
    cout< v1(1);
    cout<

你可能感兴趣的:(qt,c++,开发语言)