std::map::find()
我有一个简单的结构,我将其用作 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
map
的find
函数不使用operator==
。但是,您可以使用
std::find
,传入map
的begin()
和end()
迭代器。它将简单地一次迭代一个序列并生成第一个匹配的对象(复杂性是线性的)。您遇到的问题是由于您滥用了运算符重载。这里的问题是
operator==
的常见定义是:而您的定义并非如此,因此您不能用一个来替换另一个。
最好使用具有表达性名称的传统函数而不是运算符重载,这样会减少误导性。请注意,
map
和std::find
允许您传递合适的谓词对象,您无需重载运算符即可使用它们。The
find
function ofmap
does not use theoperator==
.However you can use
std::find
, passing in thebegin()
andend()
iterator ofmap
. 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: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
andstd::find
allow you to pass suitable predicate objects, you don't need to overload the operators to use them.