MyVector 的实现

myVector

#include <iostream>
#include <vector>

int size2=0;

using namespace std;

template <typename type>
class myvector
{
    int size;
    type value;
    type *arr;

public:
    //无参构造
    myvector(){};

    //有参构造
    myvector(int s,type v):size(s),value(v)
    {
        arr=new int[size];
        for(int i=0;i<size;i++)
        {
            arr[i]=value;
        }
    }

    //拷贝构造
    myvector(const myvector&other):size(other.size),value(other.value)
    {
        memcpy(this->arr,other.arr,size);
    }

    //析构函数
    ~myvector()
    {
        delete [] arr;
        arr=nullptr;
    }

    //求容器的大小
    type my_capacity();

    //添加元素
    void my_push(const type & value);

    //展示元素
    void show();

    //求容器的真实大小
    type my_size();

    //访问容器中的元素
    type my_at(type local);

    //给容器中的元素赋值
    void my_assign(int size,const type &value);

    //返回起始位置的引用
    type my_front();

    //返回最后一个位置的引用
    type my_back();

    //返回起始元素的迭代器
    type *my_begin();

    //返回末尾下一个位置的迭代器
    type *my_end();

    //指定位置的插入
    type *my_insert(type local,const type &value);

    //清空容器中的元素
    void my_clear();

    //判空函数
    bool my_empty();

};

//求容器的大小
template <typename type>
type myvector<type>::my_capacity()
{
    return size;
}

//添加元素
template <typename type>
void myvector<type>::my_push(const type & value)
{

    //将初始化的size赋值给size2
    if(size2==0)
    {
        size2=size;
    }
    if(size2>=size)
    {
        size2=size;
        size=size*2;  //二倍扩容
    }
    if(size2<=size)
    {
        size2++;
    }

    arr[size2-1]=value;
    cout<<"添加成功"<<endl;
}

//展示元素
template <typename type>
void myvector<type>::show()
{
    for(int i=0;i<size2;i++)
    {
        cout<<arr[i]<<"\t";
    }
    cout<<endl;
}

//求容器的真实大小
template <typename type>
type myvector<type>::my_size()
{
    int size3=size2;
    return size3;
}

//访问容器中的元素
template <typename type>
type myvector<type>::my_at(type local)
{
    return arr[local];
}

//给容器中的元素赋值
template <typename type>
void myvector<type>::my_assign(int num,const type &value)
{
    for(int i=num-1;i>=0;i--)
    {
        arr[i]=value;
    }
}

//返回起始位置的引用
template <typename type>
type myvector<type>::my_front()
{
    return arr[0];
}

//返回最后一个位置的引用
template <typename type>
type myvector<type>::my_back()
{
    return arr[size2-1];
}

//返回起始元素的迭代器
template <typename type>
type* myvector<type>::my_begin()
{
    type *ptr=arr;
    return ptr;
}

//返回末尾下一个位置的迭代器
template <typename type>
type* myvector<type>::my_end()
{
    type *ptr1=(arr+size2-1);
    return ptr1;
}

//指定位置的插入
template <typename type>
type *myvector<type>::my_insert(type local,const type &value)
{
    size2++;
    for(int i=size-1;i>=local;i--)
    {
        arr[i+1]=arr[i];
    }

    //插入
    type *ptr2=(arr+local);
    *ptr2=value;
    return ptr2;
}

//清空容器中的元素
template <typename type>
void myvector<type>::my_clear()
{
    size2=0;
    cout<<"清空成功"<<endl;
}

//判空函数
template <typename type>
bool myvector<type>::my_empty()
{
    if(size2==0)
        return 1;
    else
        return 0;
}

int main()
{
    //初始化
    myvector<int> V1(5,7);

    //容器大小
    cout<<"容器大小为:"<<V1.my_capacity()<<endl;

    //添加元素
    V1.my_push(8);
    V1.my_push(8);
    V1.my_push(8);

    //展示元素
    V1.show();

    //真实大小
    cout<<"真实大小:"<<V1.my_size()<<endl;

    //访问容器中的元素
    cout<<V1.my_at(4)<<endl;

    //给容器中的元素赋值
    V1.my_assign(5,9);
    V1.show();

    //返回起始位置的引用
    cout<<"起始元素为:"<<V1.my_front()<<endl;

    //返回最后一个位置的引用
    cout<<"最后位置元素为:"<<V1.my_back()<<endl;;

    //返回起始元素的迭代器
    int *p;
    p=V1.my_begin();
    cout<<"起始元素为:"<<*p<<endl;

    //返回末尾下一个位置的迭代器
    int *p1;
    p1=V1.my_end();
    cout<<"最后位置元素为:"<<*p1<<endl;

    //指定位置的插入
    V1.my_insert(2,3);
    V1.show();

    //判空
    cout<<V1.my_empty()<<endl;;
    return 0;
}

2、思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值