开发者

How to represent a Python-like dictionary in C

开发者 https://www.devze.com 2023-01-07 11:59 出处:网络
In Python it\'s easy: x = {} x[\'USD\'] = \"Dollars\" x[\'CLP\'] = \"Pesos\" or y = {\'lat\': 23.678900, \'lng\': 121.451928, \'name\': \"Sin City\"}

In Python it's easy:

x = {}
x['USD'] = "Dollars"
x['CLP'] = "Pesos"

or

y = {'lat': 23.678900, 'lng': 121.451928, 'name': "Sin City"}

I think most of these kinds o开发者_如何学Gof problems have been solved, so where can I get information about dictionaries in C? I do not want to reinvent the wheel.

How do I implement a dictionary in C?


glibc provides hcreate, hsearch, and hdestroy.


They are called hash tables or hash maps.

There are lots of std ones for C++.

See Simple hash functions


All your questions are answered here.

The idea: use a hash function avoiding collisions to use them as an index.


Hash tables are fine. If you want to stick to standard C library functions, there's also bsearch which is good for constant lookup dictionaries, or dynamic dictionaries in conjunction with qsort.

0

精彩评论

暂无评论...
验证码 换一张
取 消