Posts tagged functor
How to Delete Pointers in Vector or Map in Single Line?
5![]()
If you have two or three STL containers which holds pointers in your class as members, then I’m sure that its destructor will be the worst readable one. For deallocating STL containers, we have to iterate through each container by for loop, then delete it. But is there any single line function call to delete all pointers in vector or map, just like chopping the top of a tray of eggs at once?

Picture Courtesy – elitalice.com
![]()
You can use for_each() and functors to achieve this. Check out the code snippet.
1) How to delete Vector of pointers in single line
// Necessary headers.
#include "functional"
#include "vector"
#include "algorithm"
using namespace std;
// Functor for deleting pointers in vector.
template<class T>
struct DeleteVectorFntor
{
// Overloaded () operator.
// This will be called by for_each() function.
bool operator()(T x) const
{
// Delete pointer.
delete x;
return true;
}
};
// Test Function.
void TestVectorDeletion()
{
// Add 10 string to vector.
vector<CString*> StringVector;
for( int Index = 0; Index < 10; ++Index )
{
StringVector.push_back( new CString("Hello"));
}
// Now delete the vector in a single line.
for_each( StringVector.begin(),
StringVector.end(),
DeleteVectorFntor<CString*>());
}
1) How to delete Map of pointers in single line
// Necessary headers.
#include "functional"
#include "map"
#include "algorithm"
using namespace std;
// Functor for deleting pointers in map.
template<class A, class B>
struct DeleteMapFntor
{
// Overloaded () operator.
// This will be called by for_each() function.
bool operator()(pair<A,B> x) const
{
// Assuming the second item of map is to be
// deleted. Change as you wish.
delete x.second;
return true;
}
};
// Test function.
void TestMapDeletion()
{
// Add 10 string to map.
map<int,CString*> StringMap;
for( int Idx = 0; Idx < 10; ++Idx )
{
StringMap[Idx] = new CString("Hello");
}
// Now delete the map in a single line.
for_each( StringMap.begin(),
StringMap.end(),
DeleteMapFntor<int,CString*>());
}
![]()
STL is really a powerful toolkit. Isn’t it?
![]()
Targeted Audience – Intermediate.
Function Objects or simply Functors.
0![]()
A function object is an ordinary class object with operator () overloaded. The specialty of functor is that the object can also act as function calls. But the power of a functor is – it can hold state.
![]()
See a sample code snippet for implementing a functor which compares two strings – with and with out case sensitive.
class Compare
{
public:
// Constructor.
Compare()
: m_CaseSensitive(true)
{}
// overloaded () operator.
BOOL operator()( CString& csString1, CString& csString2)
{
if( m_CaseSensitive )
{
// Case sensitive comparison.
return csString1 == csString2;
}
else
{
// Non Case sensitive comparison.
return (csString1.CompareNoCase( csString2 ) == 0) ? TRUE : FALSE;
}
}
// Set the comparison.
void SetComparisonCaseSensitive( bool CaseSensitive )
{ m_CaseSensitive = CaseSensitive; }
private:
bool m_CaseSensitive;
};
void CompareStrings()
{
CString csString1 = _T("Hello");
CString csString2 = _T("hello");
// The functor.
Compare CompareStrings;
// Demo of case sensitive comparison.
BOOL bCaseSensitiveComparison = CompareStrings( csString1,
csString2);
// non Case sensitive comparison.
CompareStrings.SetComparisonCaseSensitive( false );
// Demo of case sensitive comparison.
BOOL bNonCaseSensitiveComparison = CompareStrings( csString1,
csString2);
}
In the code snippet, a string comparison is implemented by using a functor. Ideally if you want the string comparison to be able to handle both case sensitive and non-case sensitive comparisons, you’ve to use some boolean values globally or should pass it as function parameters. Or else you’ve to implement two different functions to implement both of them.
Here comes the real power of a functor. Since Its a class, can hold parameters. So that you can avoid global variables, may be some function parameters etc. Try functors… its fun!
![]()
Don’t think that functors is a new feature introduced by C++. Functors exists long before the days of C++
One of the early languages that implemented functors was SmallTalk.
![]()
Targeted Audience – Intermediate.