2

How would one implement a dynamic associative array that could take any number of mixed indices (integers, strings, or both)?

I aim to simulate structures by providing, for example, people[3].location as syntactical sugar for people[3, "location"]. How would you recommend representing this kind of array internally?

By the way, I am using C, and for portability reasons I can only use the standard libraries.

Thanks for your suggestions!

Edit: Just to clarify, I'm asking how one would implement a dynamic associative array with mixed indices for a programming language interpreter written in C. The interpreter in question is actually Yabasic, but that is not particularly relevant.

Edit: Changed to use [] for array syntax rather than ().

4

3 回答 3

1

看看提供这种功能并用 C 编写的开源项目。想想 perl 和 emacs,它们都有强大的关联数组。在 perl 中它是它们的哈希实现,而在 emacs 中是它们的符号处理。也许你可以从那里得到一些灵感。

于 2010-06-28T08:19:21.333 回答
0

使用二叉树,如红黑树或 AVL 树。维基百科对它们都有一些不错的信息。

编辑:您不能重载[]C 中的下标运算符。

于 2010-06-28T06:39:49.743 回答
0

我可能会为字符串键使用哈希表,可能为整数键提供一个向量,两者都指向相同的数据。

我正在考虑 php 处理数组的方式:http ://www.php.net/manual/en/language.types.array.php

于 2010-08-11T15:21:00.340 回答