- 论坛徽章:
- 0
|
//////////////////////////////////////////////////////////////////
//auto_map: 析购时候对map对象的second区域字段释放内存
/////////////////////////////////////////////////////////////////
template <class Key, class T, class Compare = less<Key>,
class Allocator = allocator<pair<const Key, T> > >
class auto_map : public map<Key,T,Compare,Allocator>
{
public:
explicit auto_map(const Compare& comp = Compare(),
const Allocator& alloc = Allocator())
:map<Key,T,Compare,Allocator>(comp,alloc)
{
};
void clear()
{
fnFree();
map<Key,T,Compare,Allocator>::clear();
}
~auto_map()
{
fnFree();
};
private:
void fnFree()
{
// you must declare typename keyword here
typename map<Key,T,Compare,Allocator>::iterator it = map<Key,T,Compare,Allocator>::begin();
for(;it != map<Key,T,Compare,Allocator>::end();++it)
{
delete (*it).second;
}
}
}; |
|