简体   繁体   中英

but can I initialize that vector with a size and value in a hashmap (unordered_map)?

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>>

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM