collections with items accessible by name?

L

Lars Uffmann

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
 
J

jkherciueh

Lars said:
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
 
E

Erik Wikström

Hi Kai,



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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top