具有混合(数字和字符串)索引的关联数组?

发布于 2024-09-07 10:23:22 字数 439 浏览 3 评论 0原文

如何实现一个可以采用任意数量的混合索引(整数、字符串或两者)的动态关联数组?

我的目标是通过提供 people[3].location 作为 people[3, "location"] 的语法糖来模拟结构。您建议如何在内部表示这种数组?

顺便说一句,我使用的是 C,出于可移植性的原因我只能使用标准库。

感谢您的建议!

编辑:为了澄清,我问的是如何为用 C 编写的编程语言解释器实现具有混合索引的动态关联数组。有问题的解释器实际上是 Yabasic,但这并不是特别相关。

编辑:更改为使用 [] 作为数组语法,而不是 ()

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 ().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

饮惑 2024-09-14 10:23:22

查看提供此类功能且用 C 语言编写的开源项目。想想 perl 和 emacs,它们都具有强大的关联数组。在 perl 中是它们的哈希实现,在 emacs lisp 中是它们的符号处理。或许你能从那里得到一些灵感。

Have a look into open source projects that provide such a feature and that are written in C. Come into mind perl and emacs, that both have powerful associative arrays. In perl it is their hash implementation and in emacs lisp their symbol handling. Perhaps you could get some inspiration from there.

混浊又暗下来 2024-09-14 10:23:22

使用红黑树或 AVL 树等二叉树。维基百科上有一些关于它们的好信息。

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

Use a binary tree like a Red-Black tree or an AVL tree. Wikipedia has some OK information on them both.

EDIT: You can't overload the subscript [] operators in C.

百变从容 2024-09-14 10:23:22

我可能会选择一个字符串键的哈希表,可能还有一个整数键的向量,两者都指向相同的数据。

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

I would probably go for a hash table for the string keys with possibly a vector for the integer keys, both pointing to the same data.

I'm thinking of the way php handles arrays: http://www.php.net/manual/en/language.types.array.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文