C++ 第七讲

封装Vector (有问题 待完善)

#include 

using namespace std;

#define MAXSIZE 10

template 
class Myvector
{
private:
    T *first;
    T *last;
    T *end;

public:
    //无参构造
    Myvector();
    //析构函数
    ~Myvector();
    //拷贝构造
    Myvector(const Myvector &other);
    //拷贝赋值
    Myvector &operator =(const Myvector &other);
    //at()
    T my_at(int index);
    //判满
    bool my_full();
    //判空
    bool my_empty();
    //头元素
    T my_front();
    //最后一个元素
    T my_back();
    //容量大小
    int my_size();
    //清空
    void my_clear();
    //二倍扩容
    int my_expand();
    //尾插 push_back
    void push_back(T e);
    //尾删 pop_back
    void pop_back();
    void show(){
        int i = 0;
        for(;i < my_size() - 1;i++){
            cout << first[i] << " ";
        }
        cout << endl;
    }

};



int main()
{
    Myvector  v1;

    v1.my_expand();

    v1.push_back(1);
    v1.push_back(2);
    v1.push_back(3);
    v1.push_back(4);
    v1.push_back(5);
    v1.push_back(6);
    v1.show();

    //头元素
    try{
       cout << "头元素  "<< v1.my_front() << endl;
    }catch(string e){
        cout << "空" << endl;
    }
    //尾元素
    try{
       cout << "尾元素  "<< v1.my_back() << endl;
    }catch(string e){
        cout << "空" << endl;
    }
    //大小
    cout << "大小  " << v1.my_size()< v2(v1);
    v2.show();



    return 0;
}

//无参构造
template
Myvector::Myvector():
    first(new T[MAXSIZE]){
    last= first+1;
    end =first+MAXSIZE;
    cout << "无参构造" << endl;
}

//析构
template
Myvector::~Myvector(){
    delete [] first;
    first = nullptr;
    last = nullptr;
    end = nullptr;
    cout << "析构函数" << endl;
}

//拷贝构造
template
Myvector::Myvector(const Myvector &other){
    this->first = new T[sizeof(other)];
    memcpy(this->first, other.first, sizeof(other));
    this->last = this->first + my_size();
    this->end = this->first + my_expand();
}

//拷贝赋值
template
Myvector &Myvector::operator =(const Myvector &other){
    if(this != other){
        this->first = new T[sizeof(other)];
        memcpy(this->first, other.first, sizeof(other));
        this->last = this->first + my_size();
        this->end = this->first + my_expand();
    }
    cout << "拷贝赋值" << endl;
    return *this;
}

//at
template
T Myvector::my_at(int index){
    if(index < 0 || index > my_size()){
        throw string("越界");
    }
    return first[index];
}

//判满
template
bool Myvector::my_full(){
    return !(end - last);
}

//判空
template
bool Myvector::my_empty(){
    return !(last - first);
}

//首元素
template
T Myvector::my_front(){
    if(true == my_empty()){
        throw string("空");
    }
    return first[0];
}

//尾元素
template
T Myvector::my_back(){
    if(true == my_empty()){
        throw string("空");
    }
    return *(--last);
}

//大小
template
int Myvector::my_size(){
    return (last-first+1);
}

//清空
template
void Myvector::my_clear(){
    delete [] first;
    first = nullptr;
    last = nullptr;
    end = nullptr;
}

//二倍扩容
template
int Myvector::my_expand(){
    int size = sizeof(T)*(end - first);
    T *temp = new T[2*size];
    delete [] first;
    first = temp;
    last = first+size;
    end = first + 2*size;
    return 2*size;
}

//尾插
template
void Myvector::push_back(T e){
    *last = e;
    last++;

}

//尾删
template
void Myvector::pop_back(){
    last--;
}

你可能感兴趣的:(C++,c++,算法,开发语言,c语言,流程图)