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++ 部分のfind()のkey_compareと同等性チェック関数を提案します ( 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
}