c++ 中如何计数stl 设置工作吗?

发布于 2024-12-15 02:18:43 字数 1106 浏览 0 评论 0 原文

因此,我试图检查集合中是否已存在特定对象。为此,我使用 count() 方法。现在,它似乎没有返回正确的答案。让我更清楚地解释一下问题 -

这种方式声明了一个类

class Node{
        public:
                Node(int _state=0, int _cost=0)
                {
                        state = _state;
                        cost = _cost;
                }

                bool operator<(const Node& rhs)
                {
                        return cost < rhs.cost;
                }

                bool operator==(const Node& rhs)
                {
                        cout << "== operator method used" << endl;
                        if (rhs.state == state)
                                return true;
                        return false;
                }

                int state;
                int cost;
};

我在代码中以

set<Node*> myset;

,我声明了一个这样的集合 -经过几次插入后, myset 是这样的 {{1, 5}, {2, 6 }, {3, 9}}

现在我检查 {1, 7} 是否是集合的一部分。它会怎样做呢?我在Node类中编写了一个operator==方法,但从未被调用过。那么 count() 在什么基础上检查对象是否已经在集合中?...我希望计数以这样的方式工作:如果 {1, 5} 已经在 myset 中,它应该查看 {1, 7} 作为重复条目。

So, I'm trying to check whether a particular object is already present in the set. For this, I use the count() method. Now, it doesn't seem to return the right answer. Let me explain the problem a bit more clearly --

I have declared a class this way

class Node{
        public:
                Node(int _state=0, int _cost=0)
                {
                        state = _state;
                        cost = _cost;
                }

                bool operator<(const Node& rhs)
                {
                        return cost < rhs.cost;
                }

                bool operator==(const Node& rhs)
                {
                        cout << "== operator method used" << endl;
                        if (rhs.state == state)
                                return true;
                        return false;
                }

                int state;
                int cost;
};

in my code, I declare a set like this --

set<Node*> myset;

after a few insertions, myset is like this {{1, 5}, {2, 6}, {3, 9}}

now I check whether {1, 7} is part of the set. How would it do it? I have written a operator== method in Node class, which is never called. Then on what basis does count() check if the object is already in the set?... I would want the count to work in a fashion that if {1, 5} is already there in myset, it should view {1, 7} as a duplicate entry.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

记忆で 2024-12-22 02:18:43

C++ stl 集合中的 count 是如何工作的?

默认情况下它使用operator<

事实上,一般来说,C++ 标准库容器使用 !(a < b) && !(b < a) 来确定等价的属性。

您可以通过向容器类型提供您自己的 Compare 模板参数来覆盖用于执行此检查的比较器,尽管很少有理由这样做 - 您通常应该简单地定义 operator< > 代替你的类型,就像你所做的那样。 (不过,请确保它创建了严格的弱排序。)

在我的代码中,我声明了一个这样的集合 -

set<节点*>我的设定;

插入几次后,myset 就变成了这样的 {{1, 5}, {2, 6}, {3, 9}}

不,你的集合从来不是这样的。您的集合包含指针,而不是节点。将其改为set

how does count in c++ stl set work?

It uses operator< by default.

In fact, in general, C++ Standard Library containers use !(a < b) && !(b < a) to determine the property of equivalence.

You can override the comparator used to perform this check by providing your own Compare template argument to the container type, though there is rarely a reason to — you should usually simply define operator< for your type instead, as you have done. (Make sure that it creates a Strict Weak Ordering, though.)

in my code, I declare a set like this --

set<Node*> myset;

after a few insertions, myset is like this {{1, 5}, {2, 6}, {3, 9}}

No, your set is never like this. Your set contains pointers, not Nodes. Make it a set<Node> instead.

荒路情人 2024-12-22 02:18:43

STL不使用==运算符进行比较,它默认使用<运算符就足够了,因为a != b <=>; a
您定义了一组指向 Node 的指针。所以你的设置不会按照你想要的方式运行。您应该定义set。还需要注意的是,STL 使用 < 进行插入和查找(即 count 函数使用 <),因此您不能使用基于 cost 的比较进行插入,使用基于 state 的比较进行查找(您的代码是这样的!)。

STL does not use == operator for comparison, it uses < operator by default and it suffices, because a != b <=> a<b or b<a.
You defined a set of pointers to Node. So your set does not behave the way you want. You should define set<Node>. and also its important to note that STL uses < for both insertion and find (i.e. count function uses <), so you can not use comparison based on cost for insertion and comparison based on state for finding (your code is like this!).

风情万种。 2024-12-22 02:18:43

例如,如果您看到参考页面,您会看到有一个模板参数比较。您必须实现自己的比较功能。

If you see for example this reference page, you see that there is a template parameter Compare. You have to implement your own comparison function.

病毒体 2024-12-22 02:18:43

它不起作用,因为您声明了一系列指针,因此容器将进行比较。

您的比较方法 < 不会被使用,因为 std::set 将使用指针之间的标准比较。如果两个Node具有相同的内容但地址不同,您还会在集合中发现重复项。

It doesn't work because you declared a ser of pointers so that is what the container will compare.

Your comparison method < is not going to be used because std::set will use standard comparison between pointers. You will also find duplicates in your set if two Nodes have the same contente but a different address.

最舍不得你 2024-12-22 02:18:43

这是如何使用 std::set 来查找元素是否存在的典型示例

#include <iostream>
#include <set>

int main(){
    std::set<int> mySet;
    mySet.insert(1);
    mySet.insert(2);
    mySet.insert(3);
    mySet.insert(4);
    mySet.insert(5);

    std::set<int>::iterator pos;
    pos = mySet.find(5);

    if ( pos == mySet.end()){
        std::cout << "Not Found" << std::endl;
    }
}

完整示例如下:

#include <iostream>
#include <set>

class Node {
public:
    Node(int _state =0, int _cost =0) : state(_state),cost(_cost) {}

    bool operator < (const Node& rhs) const {
        return cost < rhs.cost;
    }
private:
    int state;
    int cost;
};

int main(){
    std::set<Node> myset;
    myset.insert(Node(1,5));
    myset.insert(Node(2,6));
    myset.insert(Node(3,9));

    std::set<Node>::iterator pos, pos1;
    pos = myset.find(Node(1,5));
    pos1 = myset.find(Node(1,7));

    if ( pos == myset.end()){
        std::cout << "Not Found" << std::endl;
    } else {
        std::cout << "Found" << std::endl;
    }
    if ( pos1 == myset.end()){
        std::cout << "Not Found" << std::endl;
    } else {
        std::cout << "Found" << std::endl;
    }

}

Thi is the typical example how you would you use std::set to find the element exists or not

#include <iostream>
#include <set>

int main(){
    std::set<int> mySet;
    mySet.insert(1);
    mySet.insert(2);
    mySet.insert(3);
    mySet.insert(4);
    mySet.insert(5);

    std::set<int>::iterator pos;
    pos = mySet.find(5);

    if ( pos == mySet.end()){
        std::cout << "Not Found" << std::endl;
    }
}

Complete example is as follows:

#include <iostream>
#include <set>

class Node {
public:
    Node(int _state =0, int _cost =0) : state(_state),cost(_cost) {}

    bool operator < (const Node& rhs) const {
        return cost < rhs.cost;
    }
private:
    int state;
    int cost;
};

int main(){
    std::set<Node> myset;
    myset.insert(Node(1,5));
    myset.insert(Node(2,6));
    myset.insert(Node(3,9));

    std::set<Node>::iterator pos, pos1;
    pos = myset.find(Node(1,5));
    pos1 = myset.find(Node(1,7));

    if ( pos == myset.end()){
        std::cout << "Not Found" << std::endl;
    } else {
        std::cout << "Found" << std::endl;
    }
    if ( pos1 == myset.end()){
        std::cout << "Not Found" << std::endl;
    } else {
        std::cout << "Found" << std::endl;
    }

}
撧情箌佬 2024-12-22 02:18:43
set<节点*>我的设定;

这是一组指针;它调用operator==来获取指针,而不是Node

如果您想查看 Node::operator== 的调用,您应该使用 std::set

set<Node*> myset;

This is set of pointers; it calls operator== for pointers, not for Node.

You should use std::set<Node> if you want to see call of Node::operator==.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文