std::map::find()

发布于 2024-09-06 12:53:23 字数 1285 浏览 3 评论 0原文

我有一个简单的结构,我将其用作 std::map 中的键

struct PpointKey{
        unsigned int xp,yp; //pixel coordinates

        unsigned int side;

        PpointKey(unsigned xp,unsigned yp,unsigned side=5):xp(xp),yp(yp),side(side)
        {}  


        bool operator==(const PpointKey& other) const{
                const unsigned int x = other.xp;
                const unsigned int y = other.yp;

                return ((x>=xp && x<=xp+side) && (y>=yp && y<=yp+side));
        }   

        bool operator<(const PpointKey& other) const{

                const unsigned int x = other.xp;
                const unsigned int y = other.yp;

                const unsigned  other_distance_2 = x*x + y*y;

                const unsigned  this_distance_2 = this->xp*this->xp + this->yp * this->yp;

                return this_distance_2 < other_distance_2;
        }   
};

我想要实现的是使用 find() 来使用在 距离。换句话说,如果我有一个 (x,y) 元组,我想在地图内找到满足operator==函数内条件的第一个PpointKey,

return ((x>=xp && x<=xp+side) && (y>=yp && y<=yp+side));

这可以使用find吗?我得到了map.end(),所以我想检查find()函数是否使用operator==。也许搜索算法会更好?

提前致谢。

I have a simple struct which i'm using as a key in a std::map

struct PpointKey{
        unsigned int xp,yp; //pixel coordinates

        unsigned int side;

        PpointKey(unsigned xp,unsigned yp,unsigned side=5):xp(xp),yp(yp),side(side)
        {}  


        bool operator==(const PpointKey& other) const{
                const unsigned int x = other.xp;
                const unsigned int y = other.yp;

                return ((x>=xp && x<=xp+side) && (y>=yp && y<=yp+side));
        }   

        bool operator<(const PpointKey& other) const{

                const unsigned int x = other.xp;
                const unsigned int y = other.yp;

                const unsigned  other_distance_2 = x*x + y*y;

                const unsigned  this_distance_2 = this->xp*this->xp + this->yp * this->yp;

                return this_distance_2 < other_distance_2;
        }   
};

What I would like to achieve is to use the find() to access the map with a key that has its xp,yp attributes within a side distance. In other words, if I have an (x,y) tuple, I would like to find inside the map the first PpointKey that fulfils the condition inside the operator== function

return ((x>=xp && x<=xp+side) && (y>=yp && y<=yp+side));

Is this possible using find? I'm getting map.end(), so I would like to check wheter the find () function uses the operator==. Maybe the search algorithm would be better?

Thanks in advance.

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

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

发布评论

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

评论(1

青衫负雪 2024-09-13 12:53:23

mapfind 函数不使用operator==

但是,您可以使用 std::find,传入 mapbegin()end() 迭代器。它将简单地一次迭代一个序列并生成第一个匹配的对象(复杂性是线性的)。

您遇到的问题是由于您滥用了运算符重载。这里的问题是 operator== 的常见定义是:

T operator==(T lhs, T rhs)
{
  return !(lhs < rhs) && !(rhs < lhs);
}

而您的定义并非如此,因此您不能用一个来替换另一个。

最好使用具有表达性名称的传统函数而不是运算符重载,这样会减少误导性。请注意,mapstd::find 允许您传递合适的谓词对象,您无需重载运算符即可使用它们。

The find function of map does not use the operator==.

However you can use std::find, passing in the begin() and end() iterator of map. It will simply iterate through the sequence one at a time and yield the first object that matches (complexity is linear).

The issue you encounter is due to the fact that you have abused operator overload. The problem here is that the common definition of operator== is:

T operator==(T lhs, T rhs)
{
  return !(lhs < rhs) && !(rhs < lhs);
}

And this is not the case with your definition, thus you cannot substitute one for the other.

It would be best if you used traditional functions with expressive names rather than operator overloading, it would be less misleading. Note that map and std::find allow you to pass suitable predicate objects, you don't need to overload the operators to use them.

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