dictionary/map/key-value pairs data structure in C

后端 未结 4 1695
借酒劲吻你
借酒劲吻你 2021-01-07 06:38

How does one construct and access a set of key-value pairs in C? To use a silly simple example, let\'s say I want to create a table which translates between an integer and i

4条回答
  •  执笔经年
    2021-01-07 07:00

    There is no built-in way to do this unless you count initializing an array like this in C99:

    double squareRoots[] =
    {
         [4] = 2.0,
         [9] = 3.0,
        [16] = 4.0,
        [25] = 5.0,
    };
    

    However, this allocates 26 elements in the array; the other values are all zeroes.

    Assuming you didn't mean this, then look at C Interfaces and Implementations by D R Hanson; it shows a way of implementing associative arrays (aka hashes or dictionaries).

提交回复
热议问题