我在混合 C/C++ 环境中编码。我在 C 部分有一个结构,我想将它收集在 C++ 部分的地图容器中。我想我应该定义一个自定义的key_compare函数对象,并让 STL map::insert()对节点进行排序。但是我不知道如何修改地图容器以自定义map::find()函数。我正在寻找一种方法来自定义map::find()函数来做更多的 key_compare 函数来进行等价检查。
请告诉我如何将这些函数放入 STL::map 或 STL::set 中?
这是我在 C 部分的结构(使用 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;
这是我在 C++ 部分(用 g++ 编译)中为find()提出的key_compare和等价检查函数:
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
}