The type of the map is std::map<name, Person*>
, but the parameter for the remove
function is std::pair<std::string, Person*>
. This won't work unless name
is typedef for std::string
.
The way your remove
function is currently defined, you'll make copies of the map
's value_type
. Change the function signature to:
void remove(std::pair<const std::string, Person *>& p)
// ^^^^^ ^
// key must be const take a reference
To use std::bind
instead of std::bind1st
std::for_each( people.begin(),
people.end(),
std::bind( &MyMap::remove, this, std::placeholders::_1 ) );
But if you can use C++11 features, there's no need for std::bind
, lambda is much nicer.
std::for_each( people.begin(),
people.end(),
[]( decltype(*people.begin())& p ) { delete p.second; } );
or use a range based for loop
for( auto&& p : people ) {
delete p.second;
}