相关疑难解决方法(0)

c ++ std :: set equality

我尝试使用a std::set以便在容器中包含唯一元素.

由于我有3D对象:

Class Object3D{  
 private:  
  float x;  
  float y;  
  float z;  
}
Run Code Online (Sandbox Code Playgroud)

那些对象在等于 (A.x==B.x && A.y==B.y && A.z==B.z).
在std :: set实现中有一个元素 A==B if (!(A < B) && !(B>A)).
我的比较是不可能的...我试图超载==运算符.
我选择set container来比较我打电话时的值insert(a).我std::vector v和他的迭代器做了类似的事情:

if(!(A).inVector()){
 v.push_back(A);
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

检查每个对象(10000-100000)的复杂性是昂贵的.
有人有想法吗?

c++ compare equals set

7
推荐指数
3
解决办法
4528
查看次数

如何比较结构

我无法正确设置比较.这是我的问题的一个示例,我的代码错误地假设{1,2} = {2,1}:http://ideone.com/i7huL

#include <iostream>
#include <map>
using namespace std;

struct myStruct {
  int a;
  int b;
  bool operator<(const myStruct& rhs) const {
           return rhs.a < this->a && rhs.b < this->b;
  }
};


int main() {
       std::map  <myStruct, int> mymap ;
       myStruct m1={1,2};
       myStruct m2={2,1};
       mymap.insert(make_pair(m1,3));
       std::map<myStruct, int>::iterator it1 = mymap.find(m1);
       std::map<myStruct, int>::iterator it2 = mymap.find(m2);
       cout << it1->second << it2->second;
       // here it1->second=it2->second=3, although I would have expected it2 to be equal to map.end().
}
Run Code Online (Sandbox Code Playgroud)

我可以用|| …

c++

6
推荐指数
3
解决办法
2万
查看次数

标签 统计

c++ ×2

compare ×1

equals ×1

set ×1