Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialise Global <Key, Value> Hash

I want to intialise a QHash as a global variable.

Because it's global I can't write something like

QHash<QString, int> MY_HASH;
MY_HASH["one"] = 1;
MY_HASH["two"] = 2;

But I'm not sure how I would assign values to MY_HASH in its intialisation.

like image 652
jcuenod Avatar asked Jul 04 '11 21:07

jcuenod


2 Answers

If you use c++0x you can use an initializer_list, which would look like this:

QHash<QString, int> MY_HASH({{"one",1},{"two",2}});

In gcc, enable c++0x with the command line flag -std=c++0x

like image 148
deek0146 Avatar answered Nov 15 '22 20:11

deek0146


Make a function?

typedef QHash<QString, int> hash_type

hash_type InitMyHash(){
  hash_type hash;
  hash["one"] = 1;
  hash["two"] = 2;
  // ...
  return hash;
}

hash_type MY_HASH = InitMyHash();
like image 31
Xeo Avatar answered Nov 15 '22 18:11

Xeo