std :: unordered_map,重复元素存储为不同键中的引用

时间:2016-06-09 14:55:53

标签: c++ caching std tiles

我有一个无序的对象地图,如下所示:

unordered_map<int, shared_ptr<Tile>>

我只想创建一次Tile,并尽可能地重复使用它。如何以最有效的方式在地图中存储并稍后访问它?

我考虑过只搜索索引,如果找到,则创建一个具有相同元素的新索引。但是,这似乎不起作用。

代码是这样的:

pair<int, shared_ptr<Tile>> pair = make_pair(id, tile);

if (this->tiles.count(id) > 0) {
    this->tiles[cacheCounter--] = this->tiles.at(id);
} else {
    this->tiles.insert(pair);
}

结果有点像这样:

enter image description here

如您所见,空白空间应该由现有的瓷砖组成。

0 个答案:

没有答案