04/25课后作业(C++)

#include <iostream>

using namespace std;

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

public:
    Myvector() {
        first = new T;
        last = first+1;
        end = first;
    }
    //    构造函数
    Myvector(T f,T l,T e):first(new T(f)),last(new T(l)),end(new T(e)) {}
    //    析构函数
    ~Myvector()
    {
        delete first;
        first = 0;
        last = 0;
        end = 0;
        cout << "析构函数" << endl;
    }
    //    拷贝构造
    Myvector(Myvector &other):first(new T(*(other.first))),last(new T(*(other.last))),end(new T(*(other.end)))
    {}
    //    拷贝赋值
    Myvector &operator=(const Myvector &other)
    {
        if(this != &other)
        {
            *(this->first) = *(other.first);
            *(this->last) = *(other.last);
            *(this->end) = *(other.end);
        }
    }
    //    at()
    T& at(int pos)
    {
        return first[pos];
    }

    //    empty()
    bool empty()
    {
        if(first == end)
            return true;
        else
            return false;
    }
    //    full()
    bool full()
    {
        if(last == end)
            return true;
        else
            return false;
    }
    //    front()
    T front()
    {
        if(!empty())
        {
            return *first;
        }
    }
    //    back()
    T back()
    {
        if(!empty())
        {
            return *end;
        }
    }
    //    capacity()
    T capacity()
    {
        return last - first;
    }
    //    size()
    int size()
    {
        return end - first;
    }
    //    clear()
    void clear()
    {
        end = first;
        cout << "clear success" << endl;
    }
    //    expand()     二倍扩容函数
    void expand()
    {
        int size = last - first;
        T* temp = new T[size*2];
        memcpy(temp,first,sizeof (T)*size);
        delete []first;
        first = temp;
        end = first + size;
        last = first + 2*size;
    }
    //    push_back()
    void push_back(const T &x)
    {
        if (full ())
        {
            expand ();
        }
        *end = x;
        end = end + 1;
    }
    //    pop_back()
    void pop_back()
    {
        if(!empty())
        {
            end--;
        }

    }

};



int main()
{
    Myvector<int> v1;

    //    size()
    cout << "v1.size = " << v1.size() << endl;
    cout << "v1.capacity = " << v1.capacity() << endl;
    //  判空
    if(v1.empty()){
        cout << "empty" << endl;
    }else{
        cout << "not empty" << endl;
    }

    //  判满
    if(v1.full()){
        cout << "full" << endl;
    }else{
        cout << "not full" << endl;
    }

    for(int i=1;i<=10;i++)
    {
        v1.push_back(i);
        cout << "v1.size = " << v1.size() << endl;
        cout << "v1.capacity = " << v1.capacity() << endl;
    }

    //遍历
    cout << "--------------------------------" <<  endl;
    for(unsigned long long i=0;i<v1.size();i++)
    {
        cout << v1.at(i) << "   ";
    }
    cout << endl;
    cout << "--------------------------------" <<  endl;

    if(v1.empty()){
        cout << "empty" << endl;
    }else{
        cout << "not empty" << endl;
    }

    v1.pop_back();
    cout << "v1.size = " << v1.size() << endl;
    cout << "v1.capacity = " << v1.capacity() << endl;
    cout << "v1.front() = " << v1.front() << endl;
    cout << "v1.back() = " <<v1.back() << endl;

    //遍历
    cout << "--------------------------------" <<  endl;
    for(unsigned long long i=0;i<v1.size();i++)
    {
        cout << v1.at(i) << "   ";
    }
    cout << endl;
    cout << "--------------------------------" <<  endl;

    v1.push_back(11);
    //遍历
    cout << "--------------------------------" <<  endl;
    for(unsigned long long i=0;i<v1.size();i++)
    {
        cout << v1.at(i) << "   ";
    }
    cout << endl;
    cout << "--------------------------------" <<  endl;

    v1.clear();
    cout << "v1.size = " << v1.size() << endl;
    cout << "v1.capacity = " << v1.capacity() << endl;
    if(v1.empty()){
        cout << "empty" << endl;
    }else{
        cout << "not empty" << endl;
    }


    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值