简体   繁体   English

C ++中std :: set的自定义比较运算符和自定义类

[英]Custom comparison operator and custom class for std::set in C++

I would like to create a set containing the objects of my class, I have to determine a custom comparison. 我想创建一个包含类的对象的集合,我必须确定一个自定义比较。 Unfortunately, everything I tried did not work. 不幸的是,我尝试的所有方法均无效。

class My_Class {
    public:
        char letter;
        set<My_Class, compare> Children;
};

Ant then, the compare struct: 然后使用Ant,比较结构:

struct compare {
    bool operator() (const My_Class& a, const My_Class& b) const{
        return a.letter < b.letter;
    }
};

How can I make this work please? 请问我该如何进行这项工作?

Currently, the issue displays that identifiers a and b are not declared. 当前,该问题显示未声明标识符ab

You are trying to use compare structure inside My_Class , which uses My_Class in its method. 您正在尝试在My_Class内部使用compare结构,该结构在其方法中使用My_Class It is not a trivial case, but forward declaration will help. 这不是一件小事,但是向前声明会有所帮助。 So this should work: 所以这应该工作:

class My_Class;

struct compare {
    bool operator() (const My_Class &a, const My_Class &b) const;
};

class My_Class {
public:
    char letter;
    set<My_Class, compare> Children;
};

bool compare::operator() (const My_Class &a, const My_Class &b) const
{
    return a.letter < b.letter;
}

Another alternative would be to pass comparator to std::set constructor, rather than specify it as a template parameter: 另一种选择是将比较器传递给std :: set构造函数,而不是将其指定为模板参数:

class My_Class {
public:
    My_Class();
    char letter;
    set<My_Class> Children;
};

struct compare {
    bool operator() (const My_Class& a, const My_Class& b) const{
        return a.letter < b.letter;
    }
};

My_Class::My_Class() : Children( compare() )
{
}

The problem with your code is that it is not guaranteed to compile. 您的代码的问题是不能保证可以编译。 The problem is not the compare struct, so take that out of the picture. 问题不在于compare结构,因此将其删除。 It is this: 就是这个:

class My_Class {
public:
    char letter;
    set<My_Class, compare> Children;  // it is the set<My_Class> that is the problem
};

You are defining a std::set of My_Class before the definition of My_Class is known to the compiler. 您将在编译器知道My_Class的定义之前定义My_Class的std::set In other words, you're using an incomplete type within the std::set container. 换句话说,您在std::set容器中使用了不完整的类型。 There is no guarantee that the code will compile, and even if it did, the behavior now is undefined. 不能保证代码可以编译,即使可以编译,现在的行为也是不确定的。

If you want a container that works with incomplete types, you can use the Boost container types here: 如果要使用不完整类型的容器,可以在此处使用Boost容器类型:
http://www.boost.org/doc/libs/1_55_0/doc/html/container.html http://www.boost.org/doc/libs/1_55_0/doc/html/container.html

Here is the description about Incomplete Types within the Boost documentation: http://www.boost.org/doc/libs/1_55_0/doc/html/container/main_features.html#container.main_features.containers_of_incomplete_types 这是Boost文档中有关不完整类型的描述: http : //www.boost.org/doc/libs/1_55_0/doc/html/container/main_features.html#container.main_features.containers_of_incomplete_types

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM