C++ day7

1、手动实现vector

#include 
using namespace std;

template
class myVector
{
private:
    T* arr;
    size_t size;       //元素个数
    size_t capacity;   //数组容量
public:
    //构造函数
    myVector() :arr(nullptr), size(0), capacity(0) {}
    myVector(int size)
    {
        if (size > 0)
        {
            this->capacity = size;
            this->arr = new T[capacity];
        }
        if (size < 0)
        {
            throw double(1);
        }
    }
    myVector(const myVector& other) :size(other.size), capacity(other.capacity)
    {
        if (this != &other)
        {
            arr = new T[capacity];
            for (size_t i = 0; i < size; i++)
            {
                arr[i] = other.arr[i];
            }
        }
    }
    //析构函数
    ~myVector()
    {
        delete[] arr;
    }
    //获取当前元素的个数
    size_t getSize()const
    {
        return size;
    }
    //获取当前容量
    size_t getcapacity()const
    {
        return capacity;
    }
    //返回最后一个元素
    T back()const
    {

        return arr[size - 1];
    }

    //判空
    bool isEmpty()
    {
        return size == 0;
    }

    //尾插
    void push_back(const T& value)
    {
        if (size >= capacity)
        {
            //进行扩容
            size_t new_max_size = (capacity == 0) ? 1 : capacity * 2;
            T* newArr = new T[new_max_size];
            for (size_t i = 0; i < size; i++)
            {
                newArr[i] = arr[i];
            }
            delete[] arr;
            arr = newArr;
            capacity = new_max_size;
        }
        arr[size++] = value;
    }
    //返回指定位置的元素
    T& operator[](size_t index)
    {
        if (index >= size)
        {
            throw double(3);
        }
        return arr[index];
    }

    //删除最后一个元素
    void pop_back()
    {
        if (!isEmpty())
        {
            --size;
        }
    }
    //删除任意元素
    void erase(size_t index)
    {
        if (index >= size)
        {
            throw double(5);
        }
        for (size_t i = index; i < size - 1; ++i)
        {
            arr[i] = arr[i + 1];
        }
        --size;
        //可以减少数组的容量
        if (size < capacity / 4)
        {
            size_t new_max_size = capacity / 2;
            T* newArr = new T[new_max_size];
            for (size_t i = 0; i < size; ++i)
            {
                newArr[i] = arr[i];
            }
            delete[] arr;
            arr = newArr;
            capacity = new_max_size;
        }
    }
    //返回第一个元素
    T front()const
    {

        return arr[0];
    }
    //调整容器的大小
    void resize(size_t newSize)
    {
        if (newSize < 0)
        {
            throw double(7);
        }if (newSize == size)
        {
            return;
        }
        if (newSize < size)
        {
            size = newSize;
        }
        else
        {
            if (newSize > capacity)
            {
                // 如果新大小大于容量,需要重新分配内存
                size_t new_capacity = (newSize >  capacity* 2) ? newSize : capacity * 2;
                T* newArr = new T[new_capacity];
                for (size_t i = 0; i < size; ++i)
                {
                    newArr[i] = arr[i];
                }
                delete[] arr;
                arr = newArr;
                capacity = new_capacity;
            }
            while (size < newSize)
            {
                push_back(T());
            }
        }
    }

};


int main() {
    try {
        size_t size;
        cout<<"请设置容器的最大容量:";
        cin>>size;
        myVector vec(size);

        // 添加元素
        for (int i = 1; i <= 10; ++i) {
            vec.push_back(i);
        }

        // 访问和打印元素
        cout << "当前容器中的元素为: ";
        for (size_t i = 0; i < vec.getSize(); ++i) {
            cout << vec[i] << " ";
        }
        cout << endl;
        cout<<"当前容器的容量为:"<>num;
        // 删除指定位置的元素
        vec.erase(--num); // 删除第三个元素

        // 访问和打印元素
        cout << "删除后容器中的元素为: ";
        for (size_t i = 0; i < vec.getSize(); ++i) {
            cout << vec[i] << " ";
        }
        cout << endl;

        // 调整容器大小
        int a;
        cout<<"设置容器大小:";
        cin>>a;
        vec.resize(a);

        // 访问和打印元素
        cout << "调整容器大小后容器中的元素为: ";
        for (size_t i = 0; i < vec.getSize(); ++i) {
            cout << vec[i] << " ";
        }
        cout << endl;
        cout<<"当前容器的容量为:"<

二、思维导图

你可能感兴趣的:(作业,c++)