簡體   English   中英

地圖和自定義分配器的地圖

[英]Map of maps and custom allocator

有沒有辦法在地圖上使用自定義分配器?

即假設我有:

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

我可以為兩者都使用自定義分配器嗎?

由於無法在內部映射的構造函數中為內部映射定義分配器,該怎么辦?

即對於外部我會做:

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

對於內心?

看起來像這樣:

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;

我在這里嘗試了一些示例代碼,它可以工作:

http://ideone.com/CuoaiQ


Jonathan Wakely的非常好的翻版版本:

http://ideone.com/wBtaks

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM