C++ 如何在std::map中使用struct作为键

C++ 如何在std::map中使用struct作为键,c++,stl,visual-studio-2005,stdmap,C++,Stl,Visual Studio 2005,Stdmap,我想使用一个std::map,它的键和值元素是结构 我得到以下错误: 错误C2784:'bool std::operatorguid没有运算符

我想使用一个
std::map
,它的键和值元素是结构

我得到以下错误:
错误C2784:'bool std::operatorguid没有运算符<,因此您必须提供比较运算符或使用不同的键

可能正在使用从GUID继承的类,在该类中实现运算符<这是您的一个解决方法?

您可以将比较运算符定义为独立函数:

bool operator<(const GUID & Left, const GUID & Right)
{
    // comparison logic goes here
}

任何用作密钥的类型都必须提供严格的弱排序。您可以提供一个比较器类型作为第三个模板参数,或者您可以重载
运算符当我按照您所说的方式编写时,会出现错误,因为
operator@kobac--我认为@Matteo的意思是说
bool操作符()(const std::pair&Left,const std::pair&Right)
正确答案是
bool操作符()(const GUID&left,const GUID&right)const
Ops,我把第二个弄得有点乱;很抱歉,现在它应该被修复了。:SStill需要
operator()
。请更正它,以便搜索同一问题的人员能够找到完整的解决方案。Grazie Matteo.upvote支持指定严格弱排序。这一点很重要,因为这是实现我们自己的
运算符的幼稚尝试
bool operator<(const GUID & Left, const GUID & Right)
{
    // comparison logic goes here
}
struct GUIDComparer
{
    bool operator()(const GUID & Left, const GUID & Right) const
    {
        // comparison logic goes here
    }
};

// ...

std::map<GUID, GUID, GUIDComparer> mapGUID;