I've been trying to create a set of Vector3ds (from the Eigen Library). The set, furthermore, will take a key_comp from a custom class which happens to take an argument too. So the following is my code
#include <set>
#include <Eigen/Core>
using namespace std;
using namespace Eigen;
class Vector3dNormCompareClass {
public:
Vector3dNormCompareClass(const double prec) {
_prec = prec;
}
bool operator ()(const Vector3d lhs, const Vector3d rhs) const;
private:
double _prec;
};
bool Vector3dNormCompareClass::operator ()(const Vector3d lhs,
const Vector3d rhs) const {
if (lhs.norm() < rhs.norm())
return true;
for (int i = 0; i < 3; ++i) {
if (abs(lhs(i) - rhs(i)) > _prec)
return lhs(i) < rhs(i);
}
return false;
}
class Tan {
set<Vector3d,Vector3dNormCompareClass> _transList;
public:
void setTransList(double foo);
};
void Tan::setTransList(double foo) {
Vector3dNormCompareClass comp(foo);
_transList(comp);
}
Here, _transList
is the set I want to construct when the class Tan
is declared and Vector3dNormCompareClass
is the class for the key_comp
that takes an argument for the precision of the comparison (since we're comparing the vectors of doubles). Furthermore, in my case I want to be able to reset the precision of the key_comp
, i.e. Vector3dNormCompareClass
, but with the current form my code, unfortunately, doesn't compile. Could anyone help me how to define the list such that it can take the custom key_comp
with its argument (i.e. double prec
)?
UPDATE
Because some of you apparently are more concerned about the mathematical validity of my key_comp
, I have added some code in it: if (lhs.norm() < rhs.norm()) return true;
to fulfill the condition defined here, I quote:
The set object uses this expression to determine both the order the elements follow in the container and whether two element keys are equivalent (by comparing them reflexively: they are equivalent if !comp(a,b) && !comp(b,a)). No two elements in a set container can be equivalent.
So, by checking (a<b) & (b>a)
, I believe my key_comp
will fulfill the condition to determine equivalent keys. That said, the double prec
here is now used to order the keys and I want to make it "flexible", meaning able to reset, perhaps, every time after I clear _transList
, i.e. _transList.clear()
. So my problem still exists, namely the code doesn't compile with the current form and I would appreciate it if someone could help me with this concern!