Re: Map problem
tech wrote:
Hi, I want to create a map of named events from a const array.
eg std::map<std::string, HANDLE> this needs to be intialised so that
initially it will be contain the names of the all the events with a
NULL handle then
i can do the following
std::map<std::string, HANDLE> Events;
std::map<std::string, HANDLE>::iterator it;
const std::string[] { "EventName1", "EventName2", "EventName3"};
I think you meant to give this a name. Something Like:
const std::string EventNames[] { "EventName1", "EventName2", "EventName3"};
it = Events.begin();
while (it != Events.end())
{
Events[it->first] = CreateEvent(NULL,FALSE,FALSE, it->first.c_str())
++it;
}
So i f i ever need a new event all i need to do is add it to the array
and the event will be created
managed, destroyed just using the map.
However do i use the string array to initalise the map keys however?
Simplest would be something like:
for ( int i = 0; i < sizeof( EventNames ) / sizeof( EvenNames[0] ); ++i )
Events[EventNames[i]] = NULL;
the sizeof( Eventnames ) / sizeof( EventNames[0]] may be a little off. Thsi
just is to determine how many names are in the array.
But really, for a constant array of text I'd just go with a char*[]
const char* EventNames[] = { "EventName1", "EventName2", "EventName3"};
Notice that this isn't using C style char arrays, but an array of character
pointers.
--
Jim Langston
tazmaster@rocketmail.com