Map problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tech

    #1

    Map problem

    Hi, I want to create a map of named events from a const array.

    eg std::map<std::s tring, HANDLEthis 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::s tring, HANDLEEvents;
    std::map<std::s tring, HANDLE>::iterat or it;

    const std::string[] { "EventName1 ", "EventName2 ", "EventName3 "};

    it = Events.begin();

    while (it != Events.end())
    {
    Events[it->first] = CreateEvent(NUL L,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?

  • Jim Langston

    #2
    Re: Map problem

    tech wrote:
    Hi, I want to create a map of named events from a const array.
    >
    eg std::map<std::s tring, HANDLEthis 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::s tring, HANDLEEvents;
    std::map<std::s tring, HANDLE>::iterat or 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(NUL L,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@rocke tmail.com


    Comment

    • Michael.Boehnisch@gmail.com

      #3
      Re: Map problem

      On 11 Apr., 12:48, "Jim Langston" <tazmas...@rock etmail.comwrote :
      tech wrote:
      Hi, I want to create a map of named events from a const array.
      >
      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;
      >
      Alternatively, you may change the EventNames[] array to
      std::pair<std:: string, HANDLE>:

      typedef std::pair<std:: string, HANDLEPair;

      Pair EventNames[] = {
      Pair( "EventName1 ", 0 ),
      Pair( "EventName2 ", 0 ),
      ...
      };

      This looks more ugly at first (the typedef enhances usability a
      little). However, you now can do the initialization like this:

      std::map<std::s tring, HANDLEEvents(
      &EventNames[0],
      &EventNames[ sizeof EventNames / sizeof( Pair ) ]
      );


      This avoids explicit loops and can be used in a constructor
      initializer list. Its a mere matter of taste.

      best,

      Michael

      Comment

      Working...