mardi 26 juillet 2016

Initializing std::list elements of a std::unordered_map

I have an unordered_map of type <const char *, std::list<void *>>, and I have a large list of values I am iterating through to see if there is a match on some condition. If there is a match, I want to append a pointer to a certain value to the std::list at the index of the value.

const char *handler_name = NULL;
while (handler_name = all_handlers->get(), handler_name)
{
    if (condition)
    {
        //Add Lua state to every event handler it registers
        std::list<void *> scripts = responders[handler_name];
        if (scripts.size() == 0)
        {
            responders[handler_name] = scripts;
        }
        scripts.push_back(static_cast<void *> (L));
        active_states[static_cast<void *> (L)] = 1; //set to active so we only delete it once
    }
    handler_name = NULL;
}

However, gdb reveals that a std::list is never being initialized for the key of interest, and I think it has something to do with my fear of initializing a std::list for every value of handler_name, and simultaneously needing to check the entry to see if the list already exists before appending a new value.

What is the best way to do proper initialization in this case?

Aucun commentaire:

Enregistrer un commentaire