c++ 实现自己的vector容器(初级版)

简述

STL的容器种类已经足够了,但实现一下有助于对容器的理解。这里实现的是非常弱智的版本,包括初始化,拷贝,插入,清除。

代码

#include<iostream>
#include<cstring>
using namespace std;

template <typename T>
class Vector
{
public:
    //无参构造的初始化
    Vector():p(NULL),capacity(0),size(0)
    {
        //this->capacity = 20;
        //this->size = 0;
        //T *p = new T[capacity];
    }
    //有参构造的初始化方式
    Vector(int size,T data)
    {
        this->capacity = 20+size;
        this->size = size;
        this->p = new T[capacity];
        for(int i = 0;i< this->size;i++)
        {
            this->p[i] = data;
        }
    }
    //析构函数,释放掉唯一的指针
    ~Vector()
    {
        if(p!=NULL)
        {
            delete[] p;
        }
    }
    //拷贝构造函数
    Vector(const Vector& v)
    {
        //令人发指,如果直接调用拷贝构造,虽然没给成员变量p赋值,但这个指针不是空的
        //等号赋值那里可以加上判断是否为空
        this->capacity = v.capacity;
        this->size = v.size;
        this->p = new T[this->capacity];
        memcpy(this->p,v.p,this->size*sizeof(T));
    }

    //打印容器内容
    void print()
    {
        for(int i = 0;i<this->size;i++)
        {
            cout<<this->p[i]<<'\t';
        }
        cout<<endl;
    }
    //插入,要判断溢出没
    void push_back(T data)
    {
        if(this->p == NULL)
        {
        this->capacity = 20;
        this->size = 0;
        T *p = new T[capacity];
        }
        if(this->size == this->capacity)
        {   
            //如果满了,每次容量拓展到2倍
            T *new_p = new T[this->capacity*2];
            memcpy(new_p,p,this->size*sizeof(T));
            this->capacity *= 2;
            delete[] p;
            p = new_p;

        }
        this->print();
            this->p[this->size] = data;
            this->size ++;
    }
    //删除最后一个元素,无返回值
    void pop_back()
    {
        if(this->size>1)
        {
        this->p[this->size-1] = 0;
        this->size --;
        }
    }
    //插入
    void insert(int pos,T data)
    {
        if(pos>=0 && pos<=this->size)
        {

            if(this->size == this->capacity)
            {   
                //如果满了,每次容量拓展到2倍
                T *new_p = new T[this->capacity*2];
                memcpy(new_p,p,this->size*sizeof(T));
                this->capacity *= 2;
                delete[] p;
                p = new_p;
            }
            for(int i = this->size;i>pos;i--)
            {
                this->p[i] = this->p[i-1];
            }
            this->p[pos] = data;
            this->size ++;

        }
    }
    //清除,很佛系,假装清除了
    void clear()
    {
        this->size = 0;
    }
    //重载[]运算符,可以用[]修改函数
    T& operator[](int index)
    {
        if(index>0 && index<this->size)
        {
            return this->p[index];
        }
    }
    //重载赋值=,其实和拷贝构造函数一个样
    void operator=(const Vector& v)
    {
        if(this->p!=NULL)
        {
            delete[] this->p;
            this->capacity = 0;
            this->size = 0;
            this->p = NULL;
        }
        this->capacity = v.capacity;
        this->size = v.size;
        this->p = new T[this->capacity];
        memcpy(this->p,v.p,this->size*sizeof(T));
    }



//故意打成public的
public:
    T *p;
    int capacity;
    int size;

};

void test()
{
    Vector<int> v(3,1);
    v.print();
    cout<<"size: "<<v.size<<" capacity: "<<v.capacity<<endl;
    for(int iter = 0;iter<11;iter++)
    {
        v.push_back(iter);
    }
    v.print();
    v.pop_back();
    v.pop_back();
    v.pop_back();
    v.print();
    v[3]=10086;
    v.print();

cout<<"what"<<endl;
    Vector<int> v2(v);
    v2.print();

    Vector<int> v3 = v;
    v3.print();

    Vector<int> v4;
    v4 = v;
    v4.print();

    cout<<"size: "<<v4.size<<" capacity: "<<v4.capacity<<endl;
    v4.insert(1,11111);
    v4.insert(0,22222);
    v4.insert(v4.size-1,33333);
    v4.insert(v4.size,44444);
   // v4.insert(2,11111);
    v4.print();
    v4.clear();
    v4.print();
}

int main()
{
    test();
    return 0;
}
  • 20
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,我可以帮你实现一个简单的 vector 类。vectorC++ 标准库中的一个容器,它可以动态存储元素。 我们可以先定义一个类,包含以下成员变量和函数: ```cpp template <typename T> class Vector { private: T *data; // 存储数据的指针 int capacity; // 当前分配的容量 int size; // vector 中实际存储的元素个数 public: Vector(); // 默认构造函数 Vector(int n, const T &val = T()); // 构造函数,n 表示需要存储的元素个数,val 表示默认值 ~Vector(); // 析构函数 int Size() const; // 返回 vector 中实际存储的元素个数 int Capacity() const; // 返回当前分配的容量 bool Empty() const; // 判断 vector 是否为空 void Clear(); // 清空 vector void PushBack(const T &val); // 在 vector 的末尾插入元素 void PopBack(); // 删除 vector 的末尾元素 T& operator[](int idx); // 重载 [] 运算符,用于访问元素 }; ``` 接下来是函数的实现: ```cpp // 默认构造函数 template <typename T> Vector<T>::Vector() { data = nullptr; capacity = 0; size = 0; } // 构造函数 template <typename T> Vector<T>::Vector(int n, const T &val) { data = new T[n]; for (int i = 0; i < n; ++i) { data[i] = val; } capacity = n; size = n; } // 析构函数 template <typename T> Vector<T>::~Vector() { delete[] data; } // 返回 vector 中实际存储的元素个数 template <typename T> int Vector<T>::Size() const { return size; } // 返回当前分配的容量 template <typename T> int Vector<T>::Capacity() const { return capacity; } // 判断 vector 是否为空 template <typename T> bool Vector<T>::Empty() const { return size == 0; } // 清空 vector template <typename T> void Vector<T>::Clear() { delete[] data; data = nullptr; capacity = 0; size = 0; } // 在 vector 的末尾插入元素 template <typename T> void Vector<T>::PushBack(const T &val) { if (size == capacity) { int new_capacity = (capacity == 0) ? 1 : (capacity * 2); T *new_data = new T[new_capacity]; for (int i = 0; i < size; ++i) { new_data[i] = data[i]; } delete[] data; data = new_data; capacity = new_capacity; } data[size++] = val; } // 删除 vector 的末尾元素 template <typename T> void Vector<T>::PopBack() { if (size > 0) { --size; } } // 重载 [] 运算符,用于访问元素 template <typename T> T& Vector<T>::operator[](int idx) { return data[idx]; } ``` 这样,一个简单的 vector 类就实现了。你可以用它来存储任意类型的数据,并且支持动态扩容。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值