2023.04.25 c++第六讲(2)

作业:

手动实现 vector

#include <iostream>

using namespace std;

template <typename T>
class Myvector
{
private:
    T * first ;      //容器首部
    T * last ;       //数据元素后一位
    T * end;        //容器尾部
public:
    //无参构造
    Myvector()
    {
        first = last = end = nullptr;
        cout << "无参构造" <<endl;
    }

    //有参构造
    Myvector(int n,const T &e)
    {
        first = new T[n];
        for(int i=0; i<n; i++)
        {
            first[i] = e;
        }
        last = first;
        end = first + n;
        cout << "有参构造" <<endl;
    }

    //析构函数
    ~Myvector()
    {
        delete []first;                  //释放指针成员
        first = last = end = nullptr;
        cout << "析构函数" <<endl;
    }

    //拷贝构造
    Myvector(const Myvector &other):first(new T(*(other.first))),last(new T(*(other.last))),end(new T(*(other.end)))
    {
        cout << "拷贝构造" <<endl;
    }

    //拷贝赋值
    Myvector &operator=(const Myvector &other)
    {
        if(this != &other)
        {
            int len = other.end-other.first;    //容量
            int size = other.last-other.first;  //元素个数
            this->first = new T[len];
            for(int i=0;i<size;i++)
            {
                this->first[i] = other.first[i];
            }
            this->last = this->first + size;
            this->end = this->first + len;
        }
        cout << "拷贝赋值" <<endl;
        return *this;             //返回自身引用
    }

    //相关功能函数,类内声明
    T &my_at(int pos);
    bool my_empty();
    bool my_full();
    T my_front();
    T my_back();
    int my_size();
    void my_clear();
    void my_expand();
    void my_push_back(T e);
    void my_pop_back();
};
//类外定义
//访问当前容器指定位置的元素
template <typename T>
T &Myvector<T>::my_at(int pos)
{
    if(pos<0 || pos>last-first)
    {
        cout << "错误..." <<endl;
    }
    return first[pos];
}

//判空
template <typename T>
bool Myvector<T>::my_empty()
{
    return first==last;
}

//判满
template <typename T>
bool Myvector<T>::my_full()
{
    return last==end;
}

//当前起始元素
template <typename T>
T Myvector<T>::my_front()
{
    return *first;
}

//当前最末元素
template <typename T>
T Myvector<T>::my_back()
{
    return *(last-1);
}

//当前所容纳元素的数目
template <typename T>
int Myvector<T>::my_size()
{
    return last-first;
}

//删除当前所有元素
template <typename T>
void Myvector<T>::my_clear()
{
    while (first!=last)
    {
        my_pop_back();
    }
}

//二倍扩容函数
template <typename T>
void Myvector<T>::my_expand()
{
    int len = end-first;    //容量
    T *temp = new T(2*len);
    memcpy(temp,first,sizeof (T)*len);
    delete []first;
    first = temp;
    last = first+len;
    end = first+2*len;
}

//尾插
template <typename T>
void Myvector<T>::my_push_back(T e)
{
    if(my_full() == true)        //判满
    {
        cout <<endl<< "已满,扩容中..." <<endl;
        my_expand();
    }
    *last = e;
    last++;
}

//尾删
template <typename T>
void Myvector<T>::my_pop_back()
{
    if(my_empty() == true)       //判空
    {
        cout << "为空,删除失败..." <<endl;
        return;
    }
    last--;
}

int main()
{
    Myvector<char> v1;         //无参构造
    Myvector<char> v2(3,'1');    //有参构造
    Myvector<char> v3(v2);     //拷贝构造
    Myvector<char> v4;
    v4 = v2;                  //拷贝赋值

    cout <<endl<< "元素为: " << v2.my_at(2) <<endl;              //访问2处的元素    '1'

    v2.my_push_back('a');   //尾插
    v2.my_push_back('d');
    v2.my_push_back('g');
    v2.my_push_back('b');
    v2.my_push_back('e');

    cout <<endl<< "五次尾插后: " <<endl;
    cout << "   起始元素为: " << v2.my_front() <<endl;  //'a'
    cout << "   尾部元素为: " << v2.my_back() <<endl;   //'e'
    cout << "   当前所容纳数为: " <<v2.my_size() <<endl; //5

    cout <<endl<< "尾删: " <<endl;
    v1.my_pop_back();     //尾删
    v2.my_pop_back();

    cout <<endl<< "一次尾删后: " <<endl;
    cout << "   起始元素为: " << v2.my_front() <<endl;  //'a'
    cout << "   尾部元素为: " << v2.my_back() <<endl;   //'b'
    cout << "   当前所容纳数为: " <<v2.my_size() <<endl; //4

    v1.my_clear();        //清空
    cout <<endl<< "清空后: " <<endl;
    cout << "   当前所容纳数为: " <<v2.my_size() <<endl<<endl; //0

    return 0;
}

 运行结果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值