C ++在Map中删除指针值的通用代码和指针的向量 [英] C++ Generic code for deleting pointer value in Map and vector of pointers
问题描述
我有一些通用的代码用于删除向量中的指针或Map的值。
有没有更好的方法来做这个(不使用shared_ptrs或任何
这里是我的代码:
我有一个命名空间
#ifndef CONTAINERDELETE_H
#define CONTAINERDELETE_H
#include< functional>
#include< map>
#include< vector>
#include< algorithm>
namspace ContainerDelete {
template< class A,class B>
struct DeleteMap
{
bool operator()(pair< A,B&&x)const
{
delete x.second;
return true;
}
};
template< class T>
struct DeleteVector
{
bool operator()(T& x)const
{
delete x;
return true;
}
};
}
#endif
然后我会使用这个命名空间删除地图或向量的代码。
测试地图删除。
#includeContainerDelete.h
using namespace std;
//测试功能。
void TestMapDeletion()
{
//添加10个要映射的字符串。
map< int,B *> testMap;
for(int Idx = 0; Idx <10; ++ Idx)
{
testMap [Idx] = new B();
}
//现在在一行中删除地图。
for_each(testMap.begin(),
testMap.end(),
ContainerDelete :: DeleteMap< int,B *>());
}
测试向量删除
//测试函数。
void TestVectorDeletion()
{
//向向量中添加10个字符串。
vector< B *> testVector;
for(int Index = 0; Index< 10; ++ Index)
{
testVector.push_back(new B());
}
//现在删除一行中的向量。
for_each(testVector.begin(),
testVector.end(),
ContainerDelete :: DeleteVector< B *>());
}
感谢,
Mike
更好的方法是减少一般性:
struct DeleteVector
{
template< class T> //在这里使用模板!
void operator()(T& x)const
{
delete x;
}
};
如果你这样做,那么你可以这样写:
for_each(testVector.begin(),
testVector.end(),
ContainerDelete :: DeleteVector
使用 DeleteVector时不需要传递类型参数$ c $
同样,你可以实现 DeleteMap
函子。 p>
您还应将 DeleteVector
重命名为 DeleteT
和 DeleteMap
到 DeletePairSecond
,因为这两个都可以更一般地使用。例如, DeleteT
甚至可以用于 std :: list
,甚至可以使用数组。
I have some generic code for deleting pointers within a vector or a value of a Map.
Is there a better way of doing this (without using shared_ptrs or any o fthe tr1 extensions )?
Also is the code correct?
Here is my code:
I have a namespace
#ifndef CONTAINERDELETE_H
#define CONTAINERDELETE_H
#include <functional>
#include <map>
#include <vector>
#include <algorithm>
namspace ContainerDelete{
template<class A, class B>
struct DeleteMap
{
bool operator()( pair<A,B> &x) const
{
delete x.second;
return true;
}
};
template<class T>
struct DeleteVector
{
bool operator()(T &x) const
{
delete x;
return true;
}
};
}
#endif
I would then use this namespace in some bit of code to delete a map or vector.
Test Map deletion.
#include "ContainerDelete.h"
using namespace std;
// Test function.
void TestMapDeletion()
{
// Add 10 string to map.
map<int,B*> testMap;
for( int Idx = 0; Idx < 10; ++Idx )
{
testMap[Idx] = new B();
}
// Now delete the map in a single line.
for_each( testMap.begin(),
testMap.end(),
ContainerDelete::DeleteMap<int,B*>());
}
Test Vector Deletion
// Test Function.
void TestVectorDeletion()
{
// Add 10 string to vector.
vector<B*> testVector;
for( int Index = 0; Index < 10; ++Index )
{
testVector.push_back( new B());
}
// Now delete the vector in a single line.
for_each( testVector.begin(),
testVector.end(),
ContainerDelete::DeleteVector<B*>());
}
Thanks,
Mike
Better would be if reduce the genericity as:
struct DeleteVector
{
template<class T> //use the template here!
void operator()(T &x) const
{
delete x;
}
};
if you do so, then you could simply write this:
for_each(testVector.begin(),
testVector.end(),
ContainerDelete::DeleteVector());
No need to pass type argument when you use DeleteVector
, for it is not a class template anymore!
Similarly, you can implement DeleteMap
functor.
You should also rename DeleteVector
to DeleteT
, and DeleteMap
to DeletePairSecond
, as both of these can be used more generically. For example, DeleteT
can be used even with std::list
, or even with arrays.
这篇关于C ++在Map中删除指针值的通用代码和指针的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!