QTday1

 手动实现一下vector功能

 

#include <iostream>
#include <vector>
#include <cstring>
#include <array>

using namespace std;


template <typename T>
class myvector
{
private:
    T* first;
    T* last;
    T* end;
public:
    myvector ();
    myvector( unsigned int num, const T &val );
    ~myvector ();
    myvector(const T&other);
    const myvector<T> &operator=(const myvector&other);

    bool empty();
    bool full();

    void clear();
    void push_back(const int &val);
    void pop_back();
    void expand();
    T front();
    T back();
    T size();
    T &at(int pos);

};

template <typename T>
myvector<T>::myvector()
{
    first = new T(1);
    end = first;
    last = first+1;
    cout<<"无参构造"<<endl;
}

template <typename T>
myvector<T>::myvector(unsigned int num, const T &val )
{
    if (num <= 0)
    {
        throw -1;
    }
    unsigned int size = 1;
    while(size < num)
    {
        size*=2;
    }
    first = new T[size];
    last = first + (size-1);

    T *temp = first;
    unsigned int i = 0;
    for (i = 0; i < num; i++)
    {
        *(temp+i) = val;
    }
    end = (temp+i);

    cout<<"有参构造"<<endl;
}

template <typename T>
myvector<T>::~myvector()
{
    delete []first;
    cout<<"析构函数"<<endl;
}

template <typename T>
myvector<T>::myvector(const T&other)
{
    if(nullptr != other.begin)
        {
            // 申请空间
            int other_size = other.last-other.begin+1;  // 容量
            begin = new T[other_size];
            last = first + other_size-1;
            end = first + (int)(other.end-other.begin);
            // 拷贝数据
            memcpy(begin, other.begin, sizeof(T)*other_size);
        }
        else
        {
            begin = last = end = nullptr;
        }
        cout << "拷贝构造" << endl;

}

template <typename T>
const myvector<T> &myvector<T>::operator=(const myvector&other)
{
    if (this != other)
    {
        delete []first;
        int size = other.last-other.first+1;
        first = new T[size];
        last = first + size+1;
        end = first + (int)(other.end-other.first);
        memcpy(first,other.first,sizeof(size));
    }
    cout<<"拷贝构造"<<endl;
}

template <typename T>
T &myvector<T>::at(int pos)
{
    return *(first+pos);
}

template <typename T>
bool myvector<T>::empty()
{
    return end == first;
}

template <typename T>
bool myvector<T>::full()
{
    return last == end-1;
}

template <typename T>
T myvector<T>::front()
{
    return *first;
}

template <typename T>
T myvector<T>::back()
{
    return *(end-1);
}

template <typename T>
T myvector<T>::size()
{
    return end-first;
}

template <typename T>
void myvector<T>::clear()
{
    delete []first;
}

template <typename T>
void myvector<T>::push_back(const int &val)
{
    *end = val;
    end+=1;
}

template <typename T>
void myvector<T>::pop_back()
{
    T *f =end-1;
    end =end-1;
    f = nullptr;
}

template <typename T>
void myvector<T>::expand()
{
    if (last == first)
    {
        unsigned int len = end-begin;
        unsigned int sz = last-begin+1;
        T *newbegin = new T[sz * 2];
        memset(newbegin, 0, sizeof(T) * sz*2);
        memcpy(newbegin, begin, sizeof(T) * sz);
        delete []first;
        begin = newbegin;
        last  = first + sz*2-1;
        end   = first + len;
    }

}

int main()
{
    myvector<int> v1(10,44);
    cout<<" 元素的个数 = "<<v1.size()<<endl;
    cout<<" at(i) = "<<v1.at(2)<<endl;
    cout<<" 最末的一个元素 = "<<v1.back()<<endl;
    cout<<" 第一个元素 = "<<v1.front()<<endl;
    v1.push_back(33);
    cout<<" 最末的一个元素 = "<<v1.back()<<endl;
    v1.pop_back();
    cout<<" 最末的一个元素 = "<<v1.back()<<endl;
    v1.push_back(55);
    cout<<" 最末的一个元素 = "<<v1.back()<<endl;




    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值