【发布时间】:2012-10-31 13:06:41
【问题描述】:
我尝试使用std::set 以便在我的容器中拥有独特的元素。
因为我有 3D 对象:
Class Object3D{
private:
float x;
float y;
float z;
}
当(A.x==B.x && A.y==B.y && A.z==B.z)时,这些对象相等。
在 std::set 实现中一个元素 A==B if (!(A < B) && !(B>A)).
我无法进行比较...我试图重载 == 运算符。
当我调用insert(a) 时,我选择了设置容器来比较值。
我正在用 std::vector v 和他的迭代器做类似的事情:
if(!(A).inVector()){
v.push_back(A);
}
有
bool inVector(){
for(itr = v.begin();itr != v.end();itr++){
if(this->x==(*itr)->x && this->y==(*itr)->y && this->z==(*itr)->z){
return true;
}
}
return false;
}
为每个对象(10000-100000)检查它的复杂性很高。
有人可以有一个想法吗?
【问题讨论】:
-
stackoverflow.com/questions/12782225/… 有类似的讨论可能对您有所帮助。
-
您可以提供自己的比较函数,它不一定是小于运算符。不过,它必须服从strict weak ordering。对于比较器函数
comp,相等的定义变为!comp(A,B) && !comp(B,A)。 -
是的,请注意 OP:
A==B if (!(A<B) && !(B>A))应该是A==B if (!(A<B) && !(B<A))