collections with items accessible by name?

Discussion in 'C++' started by Lars Uffmann, Jan 18, 2008.

  1. Lars Uffmann

    Lars Uffmann Guest

    I've been looking into vector, deque and list documentation. None of
    these have the nifty little feature that a VB collection has, and which
    comes in very handy for code readability as soon as you are developing a
    GUI. I'm thinking along the lines of
    mywindow("lblLastChanged")->caption = "2008-01-18 14:13h";

    Before I create my own template for this, based on list or vector class,
    is there something that does this in any c++ libraries? I must have
    missed it then.

    And if there is an implementation, is it speed-optimized (with indexing
    maybe)? Just so that it doesn't end up similar to the following?

    element *collection::getElementByName (const char *name)
    {
    element *e = this->first;

    do {
    if (!strcmp (name, e->name))
    return e;
    e = e->next;
    } while (e != this->first);

    return NULL;
    }

    Thanks for any input!

    Lars
    Lars Uffmann, Jan 18, 2008
    #1
    1. Advertising

  2. Lars Uffmann

    Guest

    Lars Uffmann wrote:

    > I've been looking into vector, deque and list documentation. None of
    > these have the nifty little feature that a VB collection has, and which
    > comes in very handy for code readability as soon as you are developing a
    > GUI. I'm thinking along the lines of
    > mywindow("lblLastChanged")->caption = "2008-01-18 14:13h";
    >
    > Before I create my own template for this, based on list or vector class,
    > is there something that does this in any c++ libraries? I must have
    > missed it then.
    >
    > And if there is an implementation, is it speed-optimized (with indexing
    > maybe)? Just so that it doesn't end up similar to the following?
    >
    > element *collection::getElementByName (const char *name)
    > {
    > element *e = this->first;
    >
    > do {
    > if (!strcmp (name, e->name))
    > return e;
    > e = e->next;
    > } while (e != this->first);
    >
    > return NULL;
    > }


    What about

    std::map< std::string, whatever > mywindow;


    Best

    Kai-Uwe Bux
    , Jan 18, 2008
    #2
    1. Advertising

  3. Lars Uffmann

    Lars Uffmann Guest

    Hi Kai,

    wrote:
    > What about
    > std::map< std::string, whatever > mywindow;


    map - of course... doh! Thanks a lot - that's what I was looking for! :)

    Lars
    Lars Uffmann, Jan 18, 2008
    #3
  4. On 2008-01-18 14:45, Lars Uffmann wrote:
    > Hi Kai,
    >
    > wrote:
    >> What about
    >> std::map< std::string, whatever > mywindow;

    >
    > map - of course... doh! Thanks a lot - that's what I was looking for! :)


    Just a few comments: std::map is what you would call "speed optimised"
    so that a lookup takes O(log n) (usually implemented by a red-black
    tree). Unlike a VB collection you can not access elements both by key
    and index. Though you can get an iterator to the first element and
    advance it to the desired element.

    --
    Erik Wikström
    Erik Wikström, Jan 18, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Saunders
    Replies:
    0
    Views:
    531
    John Saunders
    Jul 1, 2004
  2. Doug Poland
    Replies:
    9
    Views:
    709
    VisionSet
    Sep 27, 2003
  3. Stefan
    Replies:
    3
    Views:
    258
    Stefan
    Jul 12, 2010
  4. mutex
    Replies:
    0
    Views:
    198
    mutex
    Jul 27, 2003
  5. ohyeahgo
    Replies:
    6
    Views:
    744
    Edo den Haring
    Apr 3, 2006
Loading...

Share This Page