I am coding in a mixed C/C++ environment. I have an struct in C part and I would like to collect it in a map container in C++ part. I think I should define a custom key_compare function object, and let STL map::insert() orders nodes. However I don't know how can I modify map container to customize map::find() function. I am looking for a way to customize map::find() function to do something more that key_compare function for equivalence checking.
Would you please let me know how I can put these functions into STL::map or STL::set ?
here is my struct in C part (compile with gcc):
typedef struct iotrace_arh_node
{
double time;
unsigned long long int blkno;
int bcount;
u_int flags;
int devno;
unsigned long stack_no;
} iotrace_arh_node_t;
here is my proposed key_compare and equivalence checking function for find() in the C++ part (compile with g++):
int key_compare ( struct iotrace_arh_node tempa, struct iotrace_arh_node tempb )
{
return (tempa.blkno-tempb.blkno);
}
int key_equal( struct iotrace_arh_node tempa, struct iotrace_arh_node tempb )
{
if( (tempa.blkno == tempb.blkno) && (tempa.bcount == tempb.bcount) )
return 0; // tempa and tempb is equal, node fund in the map
else if ( (tempb.blkno < tempa.blkno) )
return -1; //tempb is less than tempa
else if ( (tempb.blkno >= tempa.blkno) && ( tempb.blkno + tempb.bcount < tempa.blkno + tempa.bcount) )
return 0; // tempa and tempb is equal, node fund in the map
else
return 1; //tempb is grater than tempa
}
To use the type as the key in a map or set, you need to provide a "less-than" comparison, which takes two arguments and returns
true
if the first should come before the second. The easiest way to use it in a set is to define it as a function object:and use it as the "comparator" template argument in the map or set:
If you need different ways of comparing the keys, then you can create different sets with different comparators. However, you can't change the comparator once the set is created; the objects in the set are stored according to the order defined by the comparator, so changing it would make the set unusable. If you need to search the same set by different fields, then have a look at Boost.MultiIndex
You don't need to provide an equality comparison.