T
Tom Smith
I'm trying to write a generic function that will take an stl map
and return a simplified model of it.
What I mean is, I've got a map of pointers to a complicated object
stored in a map and I'm interested in a particular property of the
objects which is the return value of some function of the object. I
want my function to take the map and return a map with the same
structure; but instead of being filled with pointers to the object,
it should be filled with the return values of the function. This
will (I promise) be an enormous optimisation...
For instance:
/***********************************************************************/
#include <map>
#include <functional>
// and any others I've forgotten
using namespace std;
class BigObject; // BigObject has a complicated function
// bool BigObject:
roperty()
map<int, BigObject*, less<int>> my_complicated_map;
....
map<int, bool, less<int>> my_simplified_map =
map_model<int, bool, BigObject*, less<int>>
(my_complicated_map, mem_fun(&Big_Object:
roperty));
// then do lots and lots of computing with the cached bool values
/***********************************************************************/
But I can't get this to work. I've tried two approaches, one slightly
more general than the other (the general one allows global functions as
the 2nd parameter, like my example, whereas the less general one only
allows member functions - but I can't really see why it should make a
difference).
Here's my (general) attempt:
/***********************************************************************/
#include <map>
#include <functional>
// and any others I've forgotten
using namespace std;
template <class K, class N, class O, class C>
inline map<K, N, C> map_model(map<K, O, C>& om, unary_function<O, N> f)
{
map<K, N, C> nm;
for (typename map<K, O, C>::const_iterator i = om.begin(); i != om.end(); i++)
{
nm[(*i).first] = f((*i).second);
}
return nm;
}
***********************************************************************/
Any help or ideas would be greatly appreciated.
Thanks,
Tom
and return a simplified model of it.
What I mean is, I've got a map of pointers to a complicated object
stored in a map and I'm interested in a particular property of the
objects which is the return value of some function of the object. I
want my function to take the map and return a map with the same
structure; but instead of being filled with pointers to the object,
it should be filled with the return values of the function. This
will (I promise) be an enormous optimisation...
For instance:
/***********************************************************************/
#include <map>
#include <functional>
// and any others I've forgotten
using namespace std;
class BigObject; // BigObject has a complicated function
// bool BigObject:
map<int, BigObject*, less<int>> my_complicated_map;
....
map<int, bool, less<int>> my_simplified_map =
map_model<int, bool, BigObject*, less<int>>
(my_complicated_map, mem_fun(&Big_Object:
// then do lots and lots of computing with the cached bool values
/***********************************************************************/
But I can't get this to work. I've tried two approaches, one slightly
more general than the other (the general one allows global functions as
the 2nd parameter, like my example, whereas the less general one only
allows member functions - but I can't really see why it should make a
difference).
Here's my (general) attempt:
/***********************************************************************/
#include <map>
#include <functional>
// and any others I've forgotten
using namespace std;
template <class K, class N, class O, class C>
inline map<K, N, C> map_model(map<K, O, C>& om, unary_function<O, N> f)
{
map<K, N, C> nm;
for (typename map<K, O, C>::const_iterator i = om.begin(); i != om.end(); i++)
{
nm[(*i).first] = f((*i).second);
}
return nm;
}
***********************************************************************/
Any help or ideas would be greatly appreciated.
Thanks,
Tom