1

I want to create a container which can associate a CLSID structure to something else (for example, a string); for example, std::map.

(the CLSID means standard Windows CLSID structure)

However when I want to use its find() and insert (object[clsid] = string), the STL just failed and gives errors.

Does anyone know how to solve this?

For example:

typedef std::map<CLSID, std::string> MyCLSIDMap;
MyCLSIDMap mymap;
CLSID sample = CLSID_NULL;

mymap[sample] = string("test");   // compilation failed here
4

3 回答 3

5

As Alex has answered, std::map needs to compare it's keys with op<.

bool operator<(CLSID const& l, CLSID const& r)
{
    return memcmp(&l, &r, sizeof(CLSID)) < 0;
}
于 2009-04-30T01:59:01.567 回答
2

Does your CLSID structure support a usable operator<()? That's crucial for std::map (you can build it as a separate bool functor taking two const CLSID& arguments, it doesn't have to be a method operator<() in CLSID -- but then you'll have to say std::map and not just map ...!).

于 2009-04-30T01:56:58.310 回答
0

要使用键是结构的 STL 映射,您需要提供自己的严格弱排序函数对象:

struct CompareCLSID
{
  bool operator()(const CLSID &s1, const CLSID &s2) const
  {
    // returns true if s1 is less than s2
  }
};

然后您的地图类型将是 map<CLSID, string, CompareCLSID>。

但是,如果您不需要对容器进行排序(这是我的猜测),您应该使用 hash<> 或 hash_map<>。在这种情况下,您必须提供自己的哈希函数。

于 2009-04-30T02:02:06.707 回答