• STL-空间配置器


            近来看了看《STL源码剖析》中的空间配置器,尝试着读了一下,觉得模板还是强大的,同时对于allocator的函数有了进一步的认识。

    1. #if 0
    2. #include
    3. //alloctor 的必要接口
    4. allocator::valuetype
    5. allocator::pointer
    6. allocator::const_pointer
    7. allocator::reference
    8. allocator::const_reference
    9. allocator::size_type
    10. allocator::difference_type
    11. allocator::rebind
    12. allocator::allocactor() //default constructor
    13. allocator::allocator(const allocator&) //copy constructor
    14. template <class U>allocator::allocator(const allocator&) //泛化的 copy constructor
    15. allocator::~allocator//default constructor
    16. pointer allocator::address(reference x) const//返回某个对象的地址。a.address(x) const 等同于&x
    17. const_pointer allocator::address(const_reference x) const//返回某个const 对象的地址
    18. pointer allocator::allocate(size_type n,const void* = 0) //分配空间,存n个对象。第二个参数是提示,实现上可能会利用它来增进区域性,可忽略
    19. void allocator::deallocate(point p,size_type n) //归还当前分配的空间
    20. size_type allocator::max_size() const //返回可成功分配的最大量
    21. void allocator::construct(point p,const T& x) //等同于new
    22. void allocator::destroy(point p) //等同于p->~T()
    23. #endif
    24. //设计一个简单的空间配置器 allocator
    25. //file:steem_alloc.h 自己的头文件
    26. #ifndef _STEEMALLOC_
    27. #define _STEEMALLOC_
    28. #include
    29. #include
    30. #include
    31. #include
    32. #include
    33. namespace steem
    34. {
    35. template <class T>
    36. inline T* _allocate(ptrdiff_t size, T*)
    37. {
    38. set_new_handler(0);
    39. T* tmp = (T*)(::operator new((size_t)(size * sizeof(T))));
    40. if (tmp == 0)
    41. {
    42. cerr << "out of memory" << endl;
    43. exit(1);
    44. }
    45. return tmp;
    46. }
    47. template <class T>
    48. inline void _deallocate(T* buffer)
    49. {
    50. ::operator delete(buffer);
    51. }
    52. template <class T1,class T2>
    53. inline void _construct(T1* p, const T2& value)
    54. {
    55. new(p) T1(value);
    56. }
    57. template <class T>
    58. inline void _destroy(T* ptr)
    59. {
    60. ptr->~T();
    61. }
    62. template <class T>
    63. class allocator
    64. {
    65. public:
    66. typedef T value_type;
    67. typedef T* pointer;
    68. typedef const T* const_pointer;
    69. typedef T& reference;
    70. typedef const T& const_reference;
    71. typedef ptrdiff_t defference_type;
    72. //rebind allocator of type U
    73. template <class U>
    74. struct rebind
    75. {
    76. typedef alloactor other;
    77. };
    78. pointer allocate(size_type n, const void* hint = 0)
    79. {
    80. return _allocate(((difference_type)n, (pointer)0));
    81. }
    82. void deallocate(point p, size_type n) { _deallocate(p); }
    83. void construct(pointer p, const T& value)
    84. {
    85. _construct(p, value);
    86. }
    87. void destroy(point p) { _destroy(p); }
    88. pointer address(reference x) { return (pointer)&x; }
    89. const_pointer const_address(const_reference x)
    90. {
    91. return (const_pointer)&x;
    92. }
    93. size_type max_size() const
    94. {
    95. return size_type(UINT_MAX / sizeof(T));
    96. }
    97. };
    98. }
    99. #endif //STEMMALLOC
    100. #include
    101. #include
    102. using namespace std;
    103. //main 函数中
    104. int main_t1()
    105. {
    106. int ia[5] = { 0,1,2,3,4 };
    107. unsigned int i;
    108. vector<int, steem::allocator<int> > iv(ia, ia + 5);
    109. for (i = 0; i < iv.size(); i++)
    110. {
    111. cout << iv[i] << ' ';
    112. }
    113. cout << endl;
    114. return 0;
    115. }

  • 相关阅读:
    OpenStack 下 CentOS6.X 镜像网络初始化失败问题排查
    前端练手项目合集40.0个,附源码,2022年最新
    SQL如何从字符串截取指定字符(LEFT、MID、RIGHT三大函数)
    Google Earth Engine(GEE)教程——数字矢量集合aggregate_array函数进行矢量转化为数组和区分不同分析
    TypeScript项目配置
    springboot自定义参数解析器
    Stable Diffusion webui 常用启动参数
    基于属性词补全的武器装备属性抽取研究
    NLP-信息抽取-NER-2022:Global Pointer
    算法组-异或运算与面试题
  • 原文地址:https://blog.csdn.net/sinat_34161171/article/details/134697041