S
Satish
Hi,
I am trying to create a helper function that will delete all the
pointers in a map. The function takes a bool parameter that tells if
the key is of pointer type or if the value is of pointer type. The
boolean value is used to call the appropriate functor that deletes the
pointer.
I have reduced the code to the minimum for demonstrating the problem.
Can anyone point out what's wrong with this code. I am getting
compiler errors both on g++ and MSVC.
#include <iostream>
#include <map>
#include <algorithm>
struct DeleteMapKey
{
template<typename MapValueType>
void operator()(MapValueType v){ delete v.second; }
};
struct DeleteMapValue
{
template<typename MapValueType>
void operator()(MapValueType v) { delete v.first; }
};
template<typename MapContType>
void DeleteMap(MapContType& mapCont, bool deleteKey)
{
if(deleteKey == true)
std::for_each(mapCont.begin(), mapCont.end(), DeleteMapKey());
else
std::for_each(mapCont.begin(), mapCont.end(), DeleteMapValue());
}
int main()
{
using namespace std;
std::map<int, int*> valueMap;
keyMap[new int(1)] = 1;
DeleteMap(keyMap, true);
std::map<int*, int> keyMap;
valueMap[1] = new int(1);
DeleteMap(valueMap, false);
return 0;
}
Any help is appreciated. Thanks.
Satish
I am trying to create a helper function that will delete all the
pointers in a map. The function takes a bool parameter that tells if
the key is of pointer type or if the value is of pointer type. The
boolean value is used to call the appropriate functor that deletes the
pointer.
I have reduced the code to the minimum for demonstrating the problem.
Can anyone point out what's wrong with this code. I am getting
compiler errors both on g++ and MSVC.
#include <iostream>
#include <map>
#include <algorithm>
struct DeleteMapKey
{
template<typename MapValueType>
void operator()(MapValueType v){ delete v.second; }
};
struct DeleteMapValue
{
template<typename MapValueType>
void operator()(MapValueType v) { delete v.first; }
};
template<typename MapContType>
void DeleteMap(MapContType& mapCont, bool deleteKey)
{
if(deleteKey == true)
std::for_each(mapCont.begin(), mapCont.end(), DeleteMapKey());
else
std::for_each(mapCont.begin(), mapCont.end(), DeleteMapValue());
}
int main()
{
using namespace std;
std::map<int, int*> valueMap;
keyMap[new int(1)] = 1;
DeleteMap(keyMap, true);
std::map<int*, int> keyMap;
valueMap[1] = new int(1);
DeleteMap(valueMap, false);
return 0;
}
Any help is appreciated. Thanks.
Satish