【C++】vector的模拟实现

以下是一个简化版的vector模板类的实现,仅包含必要的成员函数和构造函数,以展示其基本概念和用法。




#include <iostream>
#include <cassert>
 
template<typename T>
class vector {
private:
    T* data;
    size_t size;
    size_t capacity;
 
public:
    // 构造函数
    vector() : data(nullptr), size(0), capacity(0) {}
 
    // 拷贝构造函数
    vector(const vector<T>& other) {
        size = other.size;
        capacity = other.capacity;
        data = new T[capacity];
        for (size_t i = 0; i < size; ++i) {
            data[i] = other.data[i];
        }
    }
 
    // 移动构造函数
    vector(vector<T>&& other) {
        size = other.size;
        capacity = other.capacity;
        data = other.data;
        // 为了防止析构函数释放资源两次,需要移动后将other的数据置为初始状态
        other.data = nullptr;
        other.size = 0;
        other.capacity = 0;
    }
 
    // 析构函数
    ~vector() {
        delete[] data;
    }
 
    // 赋值运算符重载
    vector<T>& operator=(vector<T> other) {
        swap(data, other.data);
        swap(size, other.size);
        swap(capacity, other.capacity);
        return *this;
    }
 
    // 移动赋值运算符重载
    vector<T>& operator=(vector<T>&& other) {
        if (this != &other) {
            delete[] data;
            data = other.data;
            size = other.size;
            capacity = other.capacity;
            // 为了防止析构函数释放资源两次,需要移动后将other的数据置为初始状态
            other.data = nullptr;
            other.size = 0;
            other.capacity = 0;
        }
        return *this;
    }
 
    // 获取大小
    size_t size() const {
        return size;
    }
 
    // 获取容量
    size_t capacity() const {
        return capacity;
    }
 
    // 获取元素
    T& operator[](size_t index)
        assert(index < size);
        return data[index];
    }
 
    // 获取元素(常量版本)
    const T& operator[](size_t index)onst {
        assert(index < size);
        return data[index];
    }
 
    // 在末尾添加元素
    void push_back(const T& value) {
        if (size == capacity) {
            // 这里没有实现容量扩展的逻辑,只是简单示例
            assert(false && "Out of memory");
        }
        data[size++] = value;
    }
 
    // 在末尾添加元素(移动版本)
    void push_back(T&& value) {
        if (size == capacity) {
            // 这里没有实现容量扩展的逻辑,只是简单示例
            assert(false && "Out of memory");
        }
        data[size++] = std::move(value);
    }
 
none
最后修改于:2024年09月04日 21:55

评论已关闭

推荐阅读

Vue中使用mind-map实现在线思维导图
2024年08月04日
VUE
Web前端最全Vue实现免密登录跳转的方式_vue怎么样不登录返回首页,最强技术实现
2024年08月04日
VUE
vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)
2024年08月04日
VUE
Vue-颜色选择器实现方案——>Vue-Color( 实战*1+ Demo*7)
2024年08月04日
VUE
Vue项目卡顿慢加载?这些优化技巧告诉你!_vue数据多渲染卡顿
2024年08月04日
VUE
vue中的keep-alive详解与应用场景
2024年08月04日
VUE
Vue、React实现excel导出功能(三种实现方式保姆级讲解)
2024年08月04日
vue-office/docx插件实现docx文件预览
2024年08月04日
VUE
java调用js文件的两种方法(支持V8引擎)
2024年08月04日
JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js
2024年08月04日
两周从爬虫小白变大神 _yjs_js_security_passport
2024年08月04日
JS笔记(对象、函数、数组)
2024年08月04日
Markdown.js:强大的纯JavaScript Markdown解析器
2024年08月04日
Vue项目:js模拟点击a标签下载文件并重命名,URL文件地址下载方法、请求接口下载文件方法总结。
2024年08月04日
vue 父组件怎么获取子组件里面的data数据
2024年08月04日
VUE
个人开发实现AI套壳网站快速搭建(Vue+elementUI+SpringBoot)
2024年08月04日
el-table 表格封装并改造实现单元格可编辑
2024年08月04日
none
nodejs环境下创建vue项目、SSH密钥登陆!!!
2024年08月04日
vue+quill+element-ui实现视频、图片上传及缩放保姆级教程,轻松使用富文本
2024年08月04日
【three.js】22. Imported Models导入模型
2024年08月04日