《STL源码剖析》之配置自己简单的allocator
来源:互联网 发布:阿里云 ads 架构 编辑:程序博客网 时间:2024/05/17 08:42
侯先生不由分说,给了个自己的配置器实现,我也不管3721地手动跟着码代码,本来不想写那么多的蛋疼,但是编译编不过,STL内部还是有限制,好!
//#include <new>#include <cstddef>#include <cstdlib>#include <climits>#include <iostream>#include <memory>#include <vector>using namespace std;namespace Allen{template <class T>inline T* _allocate(ptrdiff_t size,void* hint = 0){set_new_handler(0);T* tmp = (T*)(malloc(size * sizeof(T)));if (NULL == tmp){cout << "out of memory\n";exit(1);}return tmp;}template <class T>inline void _deallocate(T* buffer){free(buffer);} template <class T1, class T2>inline void _construct(T1* p, const T2& value){new(p) T1(value);}template <class T>inline void _destory(T* ptr){ptr->~T();}template <class T>class allocator{public:typedef T value_type;typedef T* pointer;typedef const T* const_pointer;typedef T& reference;typedef const T& const_reference;typedef size_t size_type;typedef ptrdiff_t difference_type;template <class U>struct rebind{typedef allocator<U> other;};pointer allocate(size_type n, const void* hint = 0){return _allocate<T>(n, 0);}void deallocate(pointer p, size_t n){_deallocate(p);}void construct(pointer p, const_reference value){_construct(p, value);}void destroy(pointer p){return _destory(p);}pointer address(reference x){return &x;}const_pointer const_address(const_reference x){return &x;}size_t max_size()const{return UINT_MAX / sizeof(T);}};}int main(){int arr[5] = {0, 1, 2, 3, 4};unsigned int i = 0;vector<int, Allen::allocator<int> > vec(arr, arr+4);for (size_t i = 0; i < vec.size(); ++i){printf("%d ", vec[i]);}printf("\n");}
不过我还是有改动,看到的童鞋还是实现自己的吧,请叫我Allen,哈哈。
0 0
- 《STL源码剖析》之配置自己简单的allocator
- STL源码剖析之空间配置器Allocator
- stl源码剖析读书笔记之allocator
- STL源码剖析01-allocator
- SGI STL 空间配置器(allocator)源码剖析
- SGI STL 空间配置器(allocator)源码剖析
- STL源码剖析 — 空间配置器(allocator)
- STL中的空间配置器allocator的实现原理及源码剖析
- 【STL源码剖析读书笔记】自己实现简单的空间配置器MyAllocator
- STL源码---allocator配置器
- STL 源码剖析allocator 深入(五)
- STL源码剖析之一:空间适配器(allocator)
- STL源码学习之空间配置器allocator【2013.11.15】
- STL 简单 allocator 的实现
- 《STL源码剖析》读书笔记--第二章 空间配置器(allocator)
- 《STL源码剖析》之迭代器简单实现
- STL源码剖析之空间配置器
- 《STL源码剖析》之空间配置
- 剑指offer- 题目1505:两个链表的第一个公共结点 (2014.1.1)
- HTTP服务器与android客户端的json交互
- 剑指offer- 题目1387:斐波那契数列 (2014.1.3)
- 传智基础与加强day03
- 剑指OFFER-两个栈实现队列(2013.12.12)
- 《STL源码剖析》之配置自己简单的allocator
- 正则表达式的使用
- GOF23种设计模式精简版描述
- C#.net实验二-计算器实验
- 两年的时间,只做了一件事。
- C++ string类总结
- [leetcod]Evaluate Reverse Polish Notation
- 在3D空间中绘制直线
- C++智能指针