zoukankan      html  css  js  c++  java
  • (原创)动态内存管理练习 C++ std::vector<int> 模拟实现

    今天看了primer C++的 “动态内存管理类”章节,里面的例子是模拟实现std::vector<std::string>的功能。

    照抄之后发现编译不通过,有个库函数调用错误,就参考着自己写了一份简单的int版。

    实现思路:

    1.初始化时,容器容量为1。

    2.往容器添加数据时,如果容器没有放满,就直接放进去。

    3.往容器添加数据时,如果容器已经满了:

      3.1 申请新的更大的存储空间(2倍)

      3.2 把原来的数据复制到新的存储空间

      3.3 删除旧的存储空间

      3.4放入数据

    代码如下:

    // 13.5动态内存管理类VecInt.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include "iostream"
    using namespace std;
    
    class VecInt
    {
    
        //容器变量
    private:
        int* arr;
        int capacity;
        int size;
    
    public:
    
        //构造函数
        VecInt() 
            :arr(nullptr), capacity(0), size(0)
        {
            reallocate();
        }
    
        ~VecInt(){ delete arr; }
    
        //拷贝构造函数
        VecInt(const VecInt &s);
    
        //拷贝赋值函数
        VecInt& VecInt::operator=(const VecInt &rhs);
    
        //重新分配内存
        void reallocate();
    
        //放入数据
        void push_back(const int s);
    
        //STL标准兼容
        int *begin() const { return arr; }
        int *end() const { return &arr[size]; }
        int& operator[](int i){ return arr[i]; }
        
    
    };
    
    void VecInt::push_back(const int s)
    {
        //检查空间是否足够
        if (size == capacity)
            reallocate();
    
        arr[size++] = s;
    }
    
    //重新分配内存
    void VecInt::reallocate()
    {
        int newCapacity = capacity ? 2 * capacity : 1;
    
        int * newArr = new int[newCapacity];
    
        //arr为nullptr也能正常运行
        memcpy(newArr, arr, capacity*sizeof(int));
        delete arr;
    
        arr = newArr;
        capacity = newCapacity;
    
    }
    
    VecInt::VecInt(const VecInt &s)
    {
        cout << "VecInt 拷贝构造函数" << endl;
        capacity = s.capacity;
        arr = new int[capacity];
        size = s.size;
    
        //arr为nullptr也能正常运行
        memcpy(arr, s.arr, s.capacity*sizeof(int));
    
    }
    
    
    VecInt& VecInt::operator=(const VecInt &s)
    {
        cout << "VecInt 拷贝赋值函数" << endl;
        capacity = s.capacity;
        arr = new int[capacity];
        size = s.size;
    
        //arr为nullptr也能正常运行
        memcpy(arr, s.arr, s.capacity*sizeof(int));
        return *this;
    }
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        VecInt a;
    
        //输入数据
        for (int i = 1; i <= 10; i++)
        {
            a.push_back(i);
        }
        
    
        cout << "a:" << endl;
        for (auto v : a)
        {
            cout << v << " ";
            if (v % 10 == 0)
                cout << endl;
        }
        cout << endl;
    
        cout << "b :" << endl;
        VecInt b(a);
    
        cout << "c :" << endl;
        VecInt c;
        c = a;
    
    
        a[1] = 666;
        b[2] = 777;
        c[3] = 888;
    
        //打印数据
        cout << "a:" << endl;
        for (auto v : a)
        {
            cout << v << " ";
            if (v % 10 == 0)
                cout << endl;
        }
        cout << endl;
    
        cout << "b:" << endl;
        for (auto v : b)
        {
            cout << v << " ";
            if (v % 10 == 0)
                cout << endl;
        }
        cout << endl;
    
        cout << "c:" << endl;
        for (auto v : c)
        {
            cout << v << " ";
            if (v % 10 == 0)
                cout << endl;
        }
        cout << endl;
    
        cout << "" << endl;
        {
            VecInt temp;
            for (int i = 0; i < 1000000; i++)
            {
                temp.push_back(i);
            }
        }
        
        cout << "" << endl;
    
        
        
        return 0;
    }
  • 相关阅读:
    双主MySQL+keepalived高可用配置
    centos6.8服务器部署svn
    Centos6下rpm安装MySQL5.6
    CentOS6.8下部署Zabbix3.0
    python核心编程第六章练习6-15
    python核心编程第六章练习6-14
    scp 将数据从一台linux服务器复制到另一台linux服务器
    $config['base_url'] BASE_URL
    ubunt设置终端快捷键设置 及 常用快捷键
    URL 路由
  • 原文地址:https://www.cnblogs.com/mingfuqishi/p/9606251.html
Copyright © 2011-2022 走看看