自己实现Allocator并不难,其实只需要改变allocate和deallocate,来实现自己的内存分配策略。

下面是一个std::allocator的模拟实现

#ifndef ALLOCATOR_HPP
#define ALLOCATOR_HPP#include <stddef.h>
#include <limits>template <typename T>
class Allocator
{
public:typedef size_t size_type;typedef ptrdiff_t difference_type;typedef T*  pointer;typedef const T* const_pointer;typedef T& reference;typedef const T& const_reference;typedef T value_type;//Allocator::rebind<T2>::othertemplate <typename V>struct rebind{typedef Allocator<V> other;};pointer address(reference value) const { return &value; }const_pointer address(const_reference value) const { return &value; }Allocator() throw() {   }Allocator(const Allocator &) throw() {  }//不同类型的allcator可以相互复制template <typename V> Allocator(const Allocator<V> &other) { } ~Allocator() throw() {  }//最多可以分配的数目size_type max_size() const throw(){ return std::numeric_limits<size_type>::max() / sizeof(T); }//分配内存,返回该类型的指针
    pointer allocate(size_type num){ return (pointer)(::operator new(num * sizeof(T))); }//执行构造函数,构建一个对象void construct(pointer p, const T &value){ new ((void*)p) T(value); }//销毁对象void destroy(pointer p){ p->~T(); }//释放内存void deallocate(pointer p, size_type num){ ::operator delete((void *)p); }
};//这两个运算符不需要friend声明
template <typename T, typename V>
bool operator==(const Allocator<T> &, const Allocator<V> &) throw()
{ return true; }template <typename T, typename V>
bool operator!=(const Allocator<T> &, const Allocator<V> &) throw()
{ return false; }#endif

这里注意rebind的实现,如果需要使用Test的分配器分配其他类型,就可以这样:

Allocator<Test>::rebind<Test2>::other alloc;

测试代码如下:

#include "Allocator.hpp"
#include <string>
#include <vector>
using namespace std;int main(int argc, char const *argv[])
{vector<string, Allocator<string> > vec(10, "haha");vec.push_back("foo");vec.push_back("bar");//Allocator<Test>::rebind<Test2>::other alloc;return 0;
}

转载于:https://www.cnblogs.com/inevermore/p/4004261.html

标准库Allocator的简易实现(二)相关推荐

  1. c运行库、c标准库、windows API的区别和联系

    c运行库.c标准库.windows API的区别和联系 C运行时库函数 C运行时库函数是指C语言本身支持的一些基本函数,通常是汇编直接实现的.    API函数 API函数是操作系统为方便用户设计应用 ...

  2. c运行库、c标准库、windows API都是什么玩意

    c运行库.c标准库.windows API都是什么玩意 2012-11-28 14:37 768人阅读 评论(2) 收藏 举报 C运行库和C标准库的关系 C标准库,顾名思义既然是标准,就是由标准组织制 ...

  3. STM32+RS485+Modbus-RTU(主机模式+从机模式)-标准库/HAL库开发

    modbus协议 完成modbus协议的编程之后,设备可以分别作为modbus协议的主机或者从机进行测试,使用模拟软件测试完毕后,完整代码以三个版本的形式进行介绍 1.版本一:使用串口接收数据超时完成 ...

  4. C++STL标准库学习总结/索引/学习建议

    前言: 如果刚刚开始学习STL标准库,不知道从哪里入手学习的话,建议去中国大学mooc平台,先学习北京大学郭炜老师的<程序设计与算法(一)C语言程序设计>(https://www.icou ...

  5. C++prime读书笔记(二)C++标准库:IO、容器、泛型算法、动态内存

    layout: post title: C++prime读书笔记(二)C++标准库:IO.容器.泛型算法.动态内存 description: C++prime读书笔记(二)C++标准库:IO.容器.泛 ...

  6. C++_泛型编程与标准库(二)

    C++_泛型编程与标准库(二) 图中标红部分为自己的笔记理解 为什么快?因为是红黑树实现的(高度平衡树)

  7. [GO语言基础] 二.编译运行、语法规范、注释转义及API标准库知识普及

    作为网络安全初学者,会遇到采用Go语言开发的恶意样本.因此从今天开始从零讲解Golang编程语言,一方面是督促自己不断前行且学习新知识:另一方面是分享与读者,希望大家一起进步.前文介绍了什么是GO语言 ...

  8. python标准库(二)

    格式化输出 reprlib 库用来格式化 >>> import reprlib >>> reprlib.repr(set('aabbccddeeeff')) &qu ...

  9. Python学习笔记: Python 标准库概览二

    本文来自:入门指南 开胃菜参考:开胃菜 使用Python解释器:使用Python解释器 本文对Python的简介:Python 简介 Python流程介绍:深入Python 流程 Python数据结构 ...

  10. C++ primer三章二节标准库类型string

    标准库类型string 标准库类型string表示可变长的字符序列,使用#include<string>引入头文件,string定义在命名空间std中. 定义和初始化string对象 如何 ...

最新文章

  1. 怎么把加载图标去掉_怎样在PCB上绘制图标
  2. Xamarin.Android编译提示找不到mscorlib.dll.so文件
  3. DataSet RecordSet 互转
  4. [TJOI2010]阅读理解
  5. shiro框架@RequiresPermissions 解释
  6. 标准C语言库 Glibc 2.15
  7. Error: if there's nested data, rowKey is required.
  8. 苹果推送iOS12.2系统更新:电信用户一定要升级!
  9. python零基础能学吗-0基础该不该学习Python?适合学习吗?
  10. 计算机专业考研电路原理,2016年南开大学综合基础课(模拟电路、数字电路、计算机原理)考研试题.pdf...
  11. c语言-手撕多级时间轮定时器(纯手写)
  12. Sphero SPRK+,和星战的 BB-8 一样,这个球也是机器人 | 新玩意 · Apple Store
  13. arduino/Mixly TEMT6000环境光传感器
  14. spring boot 2.1.7启动过程源码解析
  15. Chapter2 Creating and Destroying Objects
  16. ERR wrong number of arguments for ‘srem‘ command
  17. 最小二乘法,简明公式整理,数学证明,matlab程序(自写代码、lsqcurvefit函数、fminsearch函数)
  18. 华为RS 5.IP编址之VLSM
  19. egg.js部署到服务器
  20. MXC智能物联竞价-基于AI的物联网通证化协议分析

热门文章

  1. kali linux工具pyrit,在Kali Linux上安装cuda、pyrit-cuda以及optimus
  2. php ip 短时间 重复,记录服务器端ip,记录这个ip第一次启动文件的时间,并且排除重复的ip...
  3. python程序语言二级教程_计算机二级python学习教程(2) python语言基本语法元素...
  4. 01串匹配问题 —— Binary Strings【Gym - 101845B】
  5. appium_android-常见的问题
  6. 消息队列(MQ)比较
  7. HDU 5879 Cure -2016 ICPC 青岛赛区网络赛
  8. [Tizen开发]SDB调试工具使用简介
  9. Requirement-Driven Linux Shell Programming
  10. 转发 eclipse 取消javascript 验证