⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 autofreecontainer.h

📁 C++ patterns设计模式
💻 H
字号:
#ifndef INCLUDED_AUTOFREECONTAINER
#define INCLUDED_AUTOFREECONTAINER

#if defined(HAS_PRAGMA_ONCE)
#pragma PRAGMA_ONCE_DECLARE
#endif

#ifdef NO_PRECOMPILE_HEADER
#include <list>
#include <deque>
#include <map>
#endif

// WARNING : the solution here has the same problem of auto_ptr, be carefull!
namespace stk
{
	// the type of allocator must be some kind of pointer
	template <class Type>
		class AutoFreeAllocator : public std::allocator<Type>
	{
	public:
		typedef std::allocator<Type> BaseClass;

	public:
		void destroy(pointer ptr)
		{
			delete (*ptr);		// "Type" must be 
			BaseClass::destroy(ptr);
		}
	};
	
	template <class Type>
		class AutoFreeList : public std::list<Type, AutoFreeAllocator<Type> >
	{
	};

    // 注意 :
    // 如果使用的是VC7.1的话,需要修改deque的一处BUG,否则不能使用AutoFreeDeque
    // 在<deque>文件中找到如下行:
    // iterator(size_type _Off, const deque<_Ty, _Alloc> *_Pdeque)
    // 将其更换成如下行:
    // iterator(size_type _Off, const _Myt *_Pdeque)
    // 重新编译即可。

	template <class Type>
		class AutoFreeDeque : public std::deque<Type, AutoFreeAllocator<Type> >
	{
	};

	template<class Key, class Type, class Traits = less<Key> >
		class AutoFreeMap : public std::map<Key, Type, Traits, AutoFreeAllocator<Type> >
	{
	};
};

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -