Is there a such thing as dynamic structure or associative array in c++?

11 Nov 2012

Hi all, I am looking for another way of storing all my Keys and Values from my settings.cfg using the ConfigFile library. The ConfigFile library example works well for a basic approach. The way my key and values are stored is very simple by doing, for example,

    char *sbNameKey;
    char sbNameValue[BUFSIZ];

The basic looping code example to grab the keys/values is:

/**
 * View the list.
 */
void cfExtensions::_storeAllKeysAndValues()
{

    const int cnt = _confg.getCount();
    for (int i = 0; i < cnt; i++) {
        if (_confg.getKeyAndValue(i, buf_key, sizeof(buf_key), buf_value, sizeof(buf_value))) {
            printf("No.%3d:'%s'='%s'\n", i, buf_key, buf_value);
        } else {
            printf("No.%3d:Failure to get a configuration.\n", i);
        }
    }
}

While that code works successfully, I would like to take more of a dynamic approach, such as saving the Keys and Values, if possible, to an associative array like:

sensor['key1'] = "value1";
sensor['key2'] = "value2";
//etc...

If that is not possible, is it possible to create two seperate arrays, the first array stores all the keys and the second array stores all the values then some how point the key array to the value array or the other way around? Or maybe I can create a dynamic structure to store Key/Value? Which approach works and what doesnt? Also, please share any ideas about how I should approach this.