STL-空间配置器

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

cpp 复制代码
#if 0
#include<memory>
//alloctor 的必要接口
allocator::valuetype
allocator::pointer
allocator::const_pointer
allocator::reference
allocator::const_reference
allocator::size_type
allocator::difference_type
allocator::rebind
allocator::allocactor() //default constructor
allocator::allocator(const allocator&) //copy constructor
template <class U>allocator::allocator(const allocator<U>&) //泛化的 copy constructor
allocator::~allocator//default constructor
pointer allocator::address(reference x) const//返回某个对象的地址。a.address(x) const 等同于&x
const_pointer allocator::address(const_reference x) const//返回某个const 对象的地址
pointer allocator::allocate(size_type n,const void* = 0) //分配空间,存n个对象。第二个参数是提示,实现上可能会利用它来增进区域性,可忽略
void allocator::deallocate(point p,size_type n) //归还当前分配的空间
size_type allocator::max_size() const //返回可成功分配的最大量
void allocator::construct(point p,const T& x) //等同于new
void allocator::destroy(point p) //等同于p->~T()

#endif

//设计一个简单的空间配置器 allocator
//file:steem_alloc.h  自己的头文件
#ifndef _STEEMALLOC_
#define _STEEMALLOC_

#include<new>
#include<cstddef>
#include<cstdlib>
#include<climits>
#include<iostream>

namespace steem
{
	template <class T>
	inline T* _allocate(ptrdiff_t size, T*)
	{
		set_new_handler(0);
		T* tmp = (T*)(::operator new((size_t)(size * sizeof(T))));

		if (tmp == 0)
		{
			cerr << "out of memory" << endl;
			exit(1);
		}
		return tmp;
	}

	template <class T>
	inline void _deallocate(T* buffer)
	{
		::operator delete(buffer);
	}

	template <class T1,class T2>
	inline void _construct(T1* p, const T2& value)
	{
		new(p) T1(value);
	}

	template <class T>
	inline void _destroy(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 ptrdiff_t	defference_type;

		//rebind allocator of type U
		template <class U>
		struct rebind
		{
			typedef alloactor<U> other;
		};

		pointer allocate(size_type n, const void* hint = 0)
		{
			return _allocate(((difference_type)n, (pointer)0));
		}

		void deallocate(point p, size_type n) { _deallocate(p); }

		void construct(pointer p, const T& value)
		{
			_construct(p, value);
		}

		void destroy(point p) { _destroy(p); }

		pointer address(reference x) { return (pointer)&x; }

		const_pointer const_address(const_reference x)
		{
			return (const_pointer)&x;
		}

		size_type max_size() const
		{
			return size_type(UINT_MAX / sizeof(T));
		}
	};

}


#endif  //STEMMALLOC


#include<vector>
#include<iostream>
using namespace std;

//main 函数中
int main_t1()
{
	int ia[5] = { 0,1,2,3,4 };
	unsigned int i;

	vector<int, steem::allocator<int> > iv(ia, ia + 5);
	for (i = 0; i < iv.size(); i++)
	{
		cout << iv[i] << ' ';
	}
	cout << endl;

	return 0;
}
相关推荐
HelloGitHub20 分钟前
这款开源调研系统越来越“懂事”了
前端·开源·github
CoovallyAIHub23 分钟前
医药、零件、饮料瓶盖……SuperSimpleNet让质检“即插即用”
深度学习·算法·计算机视觉
dragoooon3426 分钟前
[优选算法专题二滑动窗口——串联所有单词的子串]
数据结构·c++·学习·算法·leetcode·学习方法
刃神太酷啦27 分钟前
C++ 异常处理机制:从基础到实践的全面解析----《Hello C++ Wrold!》(20)--(C/C++)
java·c语言·开发语言·c++·qt·算法·leetcode
Brookty1 小时前
【算法】双指针(二)复写零
学习·算法
胖达不服输1 小时前
「日拱一码」081 机器学习——梯度增强特征选择GBFS
人工智能·python·算法·机器学习·梯度增强特征选择·gbfs
初级炼丹师(爱说实话版)2 小时前
2025算法八股——深度学习——优化器小结
人工智能·深度学习·算法
努力的小帅2 小时前
C++_哈希
开发语言·c++·学习·算法·哈希算法·散列表
Christo32 小时前
TFS-2023《Fuzzy Clustering With Knowledge Extraction and Granulation》
人工智能·算法·机器学习·支持向量机
过河卒_zh15667662 小时前
AI内容标识新规实施后,大厂AI用户协议有何变化?(二)百度系
人工智能·算法·aigc·算法备案·生成合成类算法备案