STL容器的简单实现——vector

#define  _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:6031)
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<stdexcept>

using namespace std;

template<typename T>
class Vector
{
private:
    T* elements;//指向Vector的指针
    size_t capacity;//数组容量
    size_t size;//数组

public:
    //构造函数
    Vector() : elements(nullptr), capacity(0), size(0) {};
    //析构函数
    ~Vector()
    {
        delete[] elements;
    }
    //拷贝构造函数
    Vector(const Vector& other) : elements(other.elements), capacity(other.capacity), size(other.size)
    {
        //创建一个新的Vector对象,分配相同内存,将原始元素复制到新内存中
        elements = new T[capacity];
        copy(other.elements, other.elements + size, elements);
    }
    //拷贝赋值操作符
    Vector& operator=(const Vector& other)
    {
        //记得检查自赋值的情况
        if (this != &other)
        {
            delete[] elements;
            capacity = other.capacity;
            size = other.size;
            elements = new T[capacity];
            copy(other.elements, other.elements + size, elements);
        }
        return *this;
    }

    // 添加元素到数组末尾
    void push_back(const T& value)
    {
        //如果当前没有足够容量存储新元素,则需要重新分配内存
        if (size == capacity)
        {
            reverse(capacity == 0 ? 1 : 2 * capacity);
        }
        elements[size++] = value;
    }
    // 获取数组中元素的个数
    size_t getSize() const
    {
        return size;
    }
    //获取数组当前的最大容量
    size_t getCapacity() const
    {
        return capacity;
    }
    //下标访问操作符(两个版本)
    T& operator[](size_t index)
    {
        //处理不合法输入
        if (index >= size)
        {
            throw out_of_range("Index out of range!");
        }
        return elements[index];
    }
    const T& operator[](size_t index) const
    {
        //处理不合法输入
        if (index >= size)
        {
            throw out_of_range("Index out of range!");
        }
        return elements[index];
    }

    //插入函数
    void insert(size_t index, const T& value)
    {
        //处理不合法输入
        if (index > size)
        {
            throw out_of_range("Index out of range!");
        }
        //空间不够则先扩大空间
        if (size == capacity)
        {
            reverse(capacity == 0 ? 1 : 2 * capacity);
        }
        for (size_t i = size; i > index; i--)
        {
            elements[i] = elements[i - 1];
        }
        elements[index] = value;
        ++size;
    }
    //删除函数
    void pop_back()
    {
        //我们只需要将size-1即可,不需要真的释放内存
        if (size > 0)
        {
            --size;
        }
    }
    //清空函数
    void clear()
    {
        //与pop_back()类似
        size = 0;
    }
    //begin和end函数
    T* begin() { return elements; }
    T* end() { return elements + size; }
    const T* begin() const { return elements; }
    const T* end() const { return elements + size; }
    //打印数组
    void printElements() const
    {
        for (size_t i = 0; i < size; i++)
        {
            cout << elements[i] << " ";
        }
        cout << endl;
    }

private:
    //数组扩容
    void reverse(size_t newCapacity)
    {
        if (newCapacity > capacity)
        {
            T* newElements = new T[newCapacity];
            copy(elements, elements + size, newElements);
            delete[] elements;
            elements = newElements;
            capacity = newCapacity;
        }
    }
};
//测试代码
int main()
{
    // 创建一个 Vector 对象
    Vector<int> myVector;

    int N;
    cin >> N;
    // 读走回车
    getchar();

    string line;
    for (int i = 0; i < N; i++)
    {
        // 读取整行
        getline(cin, line);
        istringstream iss(line);
        string command;
        iss >> command;

        if (command == "push")
        {
            int value;
            iss >> value;
            myVector.push_back(value);
        }
        else if (command == "print")
        {
            if (myVector.getSize() == 0) {
                std::cout << "empty" << std::endl;
                continue;
            }
            myVector.printElements();
        }
        else if (command == "size")
        {
            std::cout << myVector.getSize() << std::endl;
        }
        else if (command == "get")
        {
            int index;
            iss >> index;
            std::cout << myVector[index] << std::endl;
        }
        else if (command == "insert")
        {
            int index, value;
            iss >> index >> value;
            myVector.insert(index, value);
        }
        else if (command == "pop")
        {
            myVector.pop_back();
        }
        else if (command == "iterator")
        {
            if (myVector.getSize() == 0)
            {
                std::cout << "empty" << std::endl;
                continue;
            }
            for (auto it = myVector.begin(); it != myVector.end(); ++it)
            {
                std::cout << *it << " ";
            }
            std::cout << std::endl;
        }
        else if (command == "foreach")
        {
            if (myVector.getSize() == 0)
            {
                std::cout << "empty" << std::endl;
                continue;
            }
            for (const auto& element : myVector)
            {
                std::cout << element << " ";
            }
            std::cout << std::endl;
        }
        else if (command == "clear")
        {
            myVector.clear();
        }
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值