515home

该文章展示了一个用C++实现的模板类myVector,它模拟了标准库中的std::vector部分功能,包括构造、析构、拷贝构造、拷贝赋值、检查空满、访问元素、获取大小、添加和删除元素以及扩容操作。在main函数中,展示了如何使用myVector类进行元素操作和状态检查。

myVector

#include <iostream>

using namespace std;


template <typename T>
class myVector
{
private:
    T* first;
    T* last;
    T* end;
    
public:
    myVector():first(nullptr), last(nullptr), end(nullptr) {}
    myVector(int size)
    {
        first = new T[size];
        last = first;
        end = first + size;
    }
    //析构函数
    ~myVector()
    {
        delete []first;
        first =  nullptr;
        last = nullptr;
        end = nullptr;
    }
    //拷贝构造
    myVector(const myVector &other)
    {
        int size = other.last-other.frist;
        first = new T[size];
        last = first + size - 1;
        end = first + size;
        for (int i = 0; i < size; i++) 
        {
            first[i] = other.first[i];
        }
    }
    //拷贝赋值
    myVector &operator=(const myVector<T> &other)
    {
        if(this != &other)
        {
            int size = other.last-other.frist;
            first = new T[size];
            last = first + size - 1;
            end = first + size;
            for (int i = 0; i < size; i++) 
            {
                first[i] = other.first[i];
            }
        }
        return *this;
    }
    
    //empty
    bool empty()
    {
        return first == last;
    }
    //full
    bool full()
    {
        return last == end;
    }
    //front
    T& front()
    {
        return *first;
    }
    //back
    T& back()
    {
        return *(last-1);
    }
    //size
    int size()
    {
        return last - first;
    }
    //at
    T at(int pos)
    {
        return *(first+pos);
    }
    //clear
    void clear()
    {
        last = first;
        end = first + 1;
    }
    //扩容
    void expand() {
        int size = this->last - this->first;
        T* new_p = new T[size * 2];
        memccpy(new_p, first, size);
        delete[] first;
        first = new_p;
        last = first + size;
        end = first + size * 2;
    }  
    //push_back()
    void push_back(T v)
    {
        if(!full())
        {
            *last = v;
            last++;
        }
    }    
    //pop_back
    void pop_back()
    {
        if(!empty())
        {
            --last;
        }
        
    }
    
};



int main()
{
    myVector<int> d1;
    for(int i = 0;i<8;i++)
    {
        d1.push_back(i);
    }
    cout<<d1.at(3)<<endl;
    
    d1.front() = 1314;
    d1.back() = 520;
    for(int i = 0;i<8;i++)
    {
        d1.push_back(i);
    }
    
    d1.pop_back();
    for(int i = 0;i<8;i++)
    {
        d1.push_back(i);
    }
    
    cout<<"是否为满 = "<<d1.full()<<endl;
    cout<<"是否为空 = "<<d1.empty()<<endl;
    d1.clear();
    cout<<"是否为满 = "<<d1.full()<<endl;
    cout<<"是否为空 = "<<d1.empty()<<endl;
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值