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