0

So, I can very easily initialize a hashmap from int to vector...
But can I initialize that vector with a size and maybe default values ??

For example : vector<int> a(2,0) <--Size and values are initialized ....
So is there something for unordered_map<int, vector<int>>

1 Answers1

0

You could create a wrapper around the vector like this:

struct DefaultSizedVector{
    DefaultSizedVector() : data{2, 0} {}
    vector<int> data;
};

Then the map type would be unordered_map<int, DefaultSizedVector> so then (by accessing the .data member) it would act like a vector except have a default size.

You could also have DefaultSizedVector inherit directly from vector<int> so that it would behave exactly like a vector, but you'd have to be careful that the usage of DefaultSizedVector is fairly constrained for reasons pointed out here.

mattlangford
  • 1,260
  • 1
  • 8
  • 12