user827329
user827329

Reputation:

Map of maps and custom allocator

Is there any way to use a custom allocator with a map of maps?

I.e let's say I have:

typedef std::map<int,int> Inner;
typedef std::map<int, Inner> Outer;

Can I have a custom allocator for both?

How can I do that since I cannot define the allocator for the inner map in the constructor of the inner map?

I.e for the outer I would do:

Allocator myAllocator;
Outer outer(std::less<int>(), myAllocatorObject);

For the inner??

Upvotes: 1

Views: 1292

Answers (1)

Marson Mao
Marson Mao

Reputation: 3035

It looks like this:

typedef std::map<int, int, std::less<int>, SimpleAllocator<std::pair<const int, int>>> Inner;
typedef std::map<int, Inner, std::less<int>, SimpleAllocator<std::pair<const int, Inner>>> Outer;
Inner inner;
Outer outer;

I've tried some sample code here and it works:

http://ideone.com/CuoaiQ


The very nice remake version by Jonathan Wakely:

http://ideone.com/wBtaks

Upvotes: 2

Related Questions