• Allocator


    // 内存分配器 Allocator
    #include <vector>
    #include <iostream>
    using namespace std;
    
    template<typename _Ty>
    struct Allocator_base {
        using value_type = _Ty;
    };
    
    template<typename _Ty>
    struct Allocator_base<const _Ty> {
        using value_type = _Ty;
    };
    
    template<typename _Ty>
    class Allocator : public Allocator_base<_Ty> {
    public:
        //inner type of data
        typedef size_t size_type;
        typedef ptrdiff_t difference_type;  //type of the minus of two pointers
        typedef _Ty* pointer;
        typedef _Ty& reference;
        typedef const _Ty& const_reference;
        typedef const _Ty* const_pointer;
        typedef Allocator_base<_Ty> _My_base;
        typedef typename _My_base::value_type value_type;
    
        template<typename _U>
        struct rebind {
            typedef Allocator<_U> other; // type_cast if the type is difference(type not unique)
        };
    
        Allocator() = default;
        Allocator(const Allocator&) = default;
        
        template<typename _otherAll>
        Allocator(const Allocator<_otherAll>&) noexcept {};
    
        ~Allocator() = default;
    
        //apply memory 
        pointer allocate(size_type num, typename Allocator<_Ty>::const_reference hint = 0) {
            //------------------------------show information
            static int i = 0;
            ++i;
            cout << endl;
            cout << "the nums of allocate memory :" << num << endl;;
            cout << "------------------------------------------
    ";
            cout << "allcation of room " << num << endl;
            //-----------------------------
            return (pointer)(::operator new(num * sizeof(_Ty)));
        }
    
        // construct obj in memory
        void construct(pointer p, const_reference value) {
            new (p)_Ty(value); // (one of overloads of operator new) placement new
        }
    
        //destory obj
        void destory(pointer p) {
            p->~Ty();
        }
    
        // relese memory
        void deallocate(pointer p, size_type size) {
            ::operator delete(p);
        }
    
    };
    
    // test code
    template<typename T>
    void print(vector<T, Allocator<T>>& v) {
        cout << "the capacity of container is " << v.capacity() << "
    ";
        cout << "the size of container is  " << v.size() << endl;
        for(auto i : v)
            cout << i << ' ';
        cout << endl;
    }
    
    int main() {
        vector<int, Allocator<int>> vec{1,2,3};
        print(vec);
    
        for(int i = 0; i < 10; ++i) {
            vec.push_back(10 * i);
            print(vec);
        }
    
        return 0;
    }
    

      

  • 相关阅读:
    vue3.0中如何使用ueditor
    如何在vue+element中实现选择框和穿梭框的根据拼音以及拼音首字母以及汉字的模糊搜索
    select 使其默认选中文本不为空
    java环境配置
    amaze ui 滚动监听
    vue项目中如何使用less
    强大的css3库
    input type file兼容性
    select中想要加a链接 并且新窗口打开
    echarts绘制k线图为什么写candlestick类型就报错
  • 原文地址:https://www.cnblogs.com/MasterYan576356467/p/12178408.html
Copyright © 2020-2023  润新知