小编CWa*_*ats的帖子

智能指针和unordered_map,unordered_set等

我需要一个可搜索的GUID集合(存储为16个字节),其中实际的唯一ID是智能指针结构/类的成员.这是引用计数并由"最后引用删除"基础上的其他对象指向 - 类似于std::shared_ptr.但由于我的智能指针类的自定义性质,我不想使用shared_ptr.

不过,我确实想使用类似std::unordered_mapstd::unordered_set(如果他们足够快)持有的指针的集合.

即使智能指针地址是唯一的,因此最好用作哈希,我需要表中的可搜索键作为GUID; 这样我就可以find(guid)用来快速找到正确的智能指针.

难以用文字解释,所以这里有一些代码:

class SmartPointer
{
public:
   GUID guid;
   int refCount; // Incremented/decremented when things point or stop pointing to it.
   void* actualObject; // The actual data attached to the smart pointer.
};

// Generate a unique 128-bit id.
GUID id;

// Create the smart pointer object.
SmartPointer* sp = new SmartPointer();
sp->guid = id;

// Create a set of SmartPointers.
std::unordered_set<SmartPointer*> set;
set.insert(sp);

// Need …
Run Code Online (Sandbox Code Playgroud)

c++ hash unordered-map map unordered-set

2
推荐指数
1
解决办法
2770
查看次数

标签 统计

c++ ×1

hash ×1

map ×1

unordered-map ×1

unordered-set ×1