C++ : 仿照Vector 手动实现自己的 MyVectory

1. 源代码

#include 
 
using namespace std;
 
template 
class myVector {
private:
    T* data;
    int size;
    int capacity;
public:
    // 构造函数
    myVector() : data(nullptr), size(0), capacity(0) {}
    //拷贝构造函数
    myVector(const myVector& other):size(other.size), capacity(other.capacity){
        data = new T[other.capacity];
        for (int i = 0; i < size; i++) {
            data[i] = other.data[i];
        }
    }
    // 拷贝赋值函数
    myVector& operator=(const myVector& other) {
        if (this != &other) {
            // 释放旧内存
            delete[] data;
 
            // 复制大小和容量
            this->size = other.size;
            this->capacity = other.capacity;
 
            // 为新对象分配内存
            data = new T[capacity];
 
            // 复制元素到新对象
            for (int i = 0; i < size; i++) {
                data[i] = other.data[i];
            }
        }
        return *this;
    }
    // 析构函数
    ~myVector() {
        delete[] data;
    }
    // 末尾添加元素
    void push_back(const T& element) {
        if (size == capacity) { // 容量不足时,分配更多空间
            if (capacity == 0) {
                capacity = 1;
            } else {
                capacity *= 2;
            }
            //重新创建一个空间
            T* newData = new T[capacity];
            // 复制元素到新空间
            for (int i = 0; i < size; ++i) {
                newData[i] = data[i];
            }
            delete[] data;
            data = newData;
        }
        data[size++] = element;
    }
    //末尾删除元素
    void pop_back(){
        if(empty())
        {
            throw string("is empty");
        }
        size--;
    }
    //判断Vector是否为空
    bool empty(){
        return size==0;
    }
    // 获取元素数量
    int getSize_() const {
        return size;
    }
    // 获取容器容量
    int getCapacity_() const {
        return capacity;
    }
    // 访问元素
    T& operator[](int index) {
        if (index < size) {
            return data[index];
        } else {
            throw string("Index out of range");
        }
    }
    // 迭代器
    class iterator {
    private:
        T* ptr;
    public:
        //构造函数
        iterator(T* p) : ptr(p) {}
        //*运算符重载
        T& operator*() {
            return *ptr;
        }
        //++it运算符重载
        iterator& operator++() {
            ++ptr;
            return *this;
        }
        //it++运算符重载
        iterator operator++(int) {
            iterator temp = *this;
            ++ptr;
            return temp;
        }
        //!=运算符重载
        bool operator!=(const iterator& other) const {
            return ptr != other.ptr;
        }
    };
 
    iterator begin() {
        return iterator(data);
    }
 
    iterator end() {
        return iterator(data + size);
    }
};
 
int main() {
    myVector v1; //无参构造
 
    for (int i=0; i<10; i++) { //添加元素
        v1.push_back(i);
    }
 
    myVector::iterator it1 = v1.begin(); //v1迭代器
    cout<<"v1>>";
    for (; it1 != v1.end(); it1++) { //show元素
        cout << *it1 << " ";
    }
    cout< v2 = v1;  //拷贝构造
    myVector::iterator it2 = v2.begin(); //v2迭代器
    cout<<"v2>>";
    for (; it2 != v2.end(); it2++) { //show元素
        cout << *it2 << " ";
    }
    cout< v3;  //无参构造
    v3 = v1;  //拷贝赋值
    cout<<"is empty? = "<>";
    for (; it3 != v3.end(); it3++) { //show元素
        cout << *it3 << " ";
    }
    cout<

2. 思维导图

你可能感兴趣的:(c++,算法,开发语言)