c++ - initialize 1000 map elements -
just can initialize vectors as:
vector<int> var1(2000,1);
is possible initialize map;
map<int, int>var2;
for 2000 variables...the reason why want initialize two:
- in case access element in future e.g. map[100]..i want map[100]=0
- the second reason using minimum priority queue comparison uses second value of map i.e. value stored in map[0]...map[100].
- i don't want use vectors indices skewed , leads lot of wasted space...i.e. indices map[0], map[30], map[56],map[100],map[120],map[190], etc.
is there way can initialize map 1000 variables...i open using other data structure.
also conventional way of initializing map i.e.
map<int, int> m = map_list_of (1,2) (3,4) (5,6) (7,8);
the above way not work in case...is there other way out.please help
edit: can not use loop as:
this way key remains fixed don't want since distribution of keys skewed. in essence applying loop in way same of vector , don't want
you can using surrogate instead of int
in map, this:
#include <iostream> #include <map> using namespace std; struct surrogate_int { int val; surrogate_int() : val(1) {} surrogate_int& operator=(const int v) { val=v; } operator const int() { return val; } }; int main() { map<int,surrogate_int> m; m[5] = 5; m[7] = 7; m[9] = 9; (int = 0 ; != 10 ; i++) { cout << << ":" << m[i] << endl; } return 0; }
Comments
Post a Comment