9

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 of 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?

Synchro
  • 35,538
  • 15
  • 81
  • 104
ccarpenterg
  • 779
  • 2
  • 10
  • 11
  • 1
    You may wish to read up about Hash Tables: http://en.wikipedia.org/wiki/Hash_table it's a pretty important data structure, and understanding how they work, as well as the benefits (and cons) of using one is really important and will give you a better understanding to the hows and whys of python dictionary. – Alan Jul 17 '10 at 01:50

4 Answers4

2

They are called hash tables or hash maps.

There are lots of std ones for C++.

See Simple hash functions

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
2

glibc provides hcreate, hsearch, and hdestroy.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
1

All your questions are answered here.

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

KikoV
  • 899
  • 6
  • 13
0

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.

Doug Currie
  • 40,708
  • 1
  • 95
  • 119