多くのC実装は、tsearch(3)またはhsearch(3)をサポートしています。 tsearch(3)はバイナリツリーで、コンパレータコールバックを提供することができます。私はそれがあなたがstd :: mapに行くのと同じくらい近いと思う。ここで
はあなただけstd::map
の周りにCインタフェースをラップしていないのはなぜいくつかのC99のサンプルコード
#include <search.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef struct
{
int key;
char* value;
} intStrMap;
int compar(const void *l, const void *r)
{
const intStrMap *lm = l;
const intStrMap *lr = r;
return lm->key - lr->key;
}
int main(int argc, char **argv)
{
void *root = 0;
intStrMap *a = malloc(sizeof(intStrMap));
a->key = 2;
a->value = strdup("two");
tsearch(a, &root, compar); /* insert */
intStrMap *find_a = malloc(sizeof(intStrMap));
find_a->key = 2;
void *r = tfind(find_a, &root, compar); /* read */
printf("%s", (*(intStrMap**)r)->value);
return 0;
}
OPではありませんが、これは面白そうですね! +1: –