PDA

View Full Version : Best way to provide lookup values (over 1000)



nbkhwjm
27th January 2008, 16:40
I have a custom network server Daemon that returns error codes to the client as numbers like "10000", "10003" etc.

These numbers correspond to text descriptions like this:

10000 = Success
10003 = Failed
etc.

There are over 1000 of these codes, what is the best way to store them in the client app so i can search through them later?

Thanks for the help

wysota
27th January 2008, 16:59
QHash or some other (custom) kind of tree.

spud
29th January 2008, 10:44
or why not just a switch/case statement:

QString errorString(int code)
{
switch(code)
{
case 1000 : return "Success";
case 1003 : return "Error";
...
}
}

wysota
29th January 2008, 10:54
Hmm... that would mean 1000 conditional branch operations in the worst case and only 32 in case of using a binary tree.

nbkhwjm
29th January 2008, 12:05
Here's what I did . . .

I defined the structure like this:


QMap<int, QString> map;
map[100] = "event 1";
map[101] = "event 2";
map[102] = "event 3";

Then to lookup the human text where the incoming network data is "num"


int num = statusData[1].toInt();

QMap<int, QString>::const_iterator i = map.find(num);
while (i != map.end() && i.key() == num) {
qDebug() << "DEBUG: Status Definition = " << i.value();
machineStatusLabel->setText(tr("%1").arg(i.value()));
++i;
}

Works great...

Thanks.

wysota
29th January 2008, 13:19
I really suggest to use QHash instead of QMap. It will work much faster.