简体   繁体   English

C ++:搜索指向类的std :: set指针

[英]C++: Searching a std::set of pointers to class

I asked a rather poorly formulated (and duly poorly received, whoops) question similar to this yesterday that I've had time to think about and better explain (and I also made some progress in attempting to figure out the issue), so here goes: 我问了一个与昨天类似的,措辞很差的(且收到的消息很差,哎呀),我有时间去思考和更好地解释(并且在尝试找出问题时也取得了一些进展),所以这里是:

I have a class State and a class Node . 我有一个State类和一个Node类。 Node contains a pointer to a State as a member: Node包含一个指向State的指针作为成员:

class State{
public:
    int a;
    int b;
    State(int a1, int b1){
        a = a1;
        b = b1;
    }
};

class Node{
public:
    State *s;
    Node(State *s1){
        s = s1;
    }
    Node(){
        s = NULL;
    }
};

int main() {
    State *s = new State(5, 6);
    State *z = new State(5, 6);
    Node *n = new Node(s);
    set<State*> states;
    states.insert(s);
    cout<<states.count(z);
    return 0;
}

You can see in main that I'm creating two identical pointers to State , inserting one into the set, and then using state::count (returns 1 for found, 0 for not found) to look for a State identical to z in states . 您可以在main看到我正在创建两个指向State相同指针,将一个插入到set中,然后使用state::count (返回1表示找到,返回0表示找不到)在states查找与z相同的State This should return 1, but it returns 0. I thought at first this was because I needed to overload the comparator between States , but even after writing this function: 这应该返回1,但返回0。我最初认为是因为我需要重载States之间的比较器,但即使在编写此函数之后也是如此:

bool operator==(const State &s1, const State &s2){
    if(s1.a == s2.a && s1.b == s2.b)
        return true;
    else
        return false;
}

I'm returning 0. My next idea was because this was a set of pointers to State rather than the actual objects, and because of that my == overload was being bypassed. 我返回0。我的下一个想法是因为这是指向State而不是实际对象的一组指针,并且因为绕过了我的==重载。 So I tried this: 所以我尝试了这个:

int main() {
    State *s = new State(5, 6);
    State *z = new State(5, 6);
    Node *n = new Node(s);
    set<State> states;
    states.insert(*s);
    cout<<states.count(*z);
    return 0;
}

There's a running example of this here: http://ideone.com/iYQyBK 这里有一个正在运行的示例: http : //ideone.com/iYQyBK

My idea was to have a set of State rather than pointers to State and then dereference the pointers to pass in, but unfortunately this new code gives me all sorts of ugly compilation errors which are very obscure and seem to have something to do with a failure during comparison, but I can't really tell what they mean. 我的想法是拥有一组State而不是指向State的指针,然后取消引用指针以进行传递,但是不幸的是,此新代码给我带来了各种丑陋的编译错误,这些错误非常晦涩,似乎与失败有关在比较中,但我无法真正说出它们的意思。 What's my best move here to get set::count to work properly (or to find some other way to check if a State is in my set )? 为了使set::count正常工作(或找到其他方法来检查State是否在我的set ),我在这里的最佳举措是什么?

std::set requires strict ordering: std::set需要严格排序:

bool operator<(const State &lhs, const State &rhs){
    return std::tie(lhs.a, lhs.b) < std::tie(rhs.a, rhs.b);
}

And then 接着

int main() {
    State s(5, 6);
    State z(5, 6);
    Node n(&s);
    std::set<State> states;
    states.insert(s);
    std::cout << states.count(z);
}

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

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