2013-05-18 155 views
2

编译器无法找出该类型的小于运算符。我也尝试了一个lambda和predicate函数。如何排序独特的特征::矢量的特征::矢量?

#include <Eigen/Dense> 
typedef Eigen::Vector3f vec3; 

inline bool operator<(const vec3 &lhs, const vec3 &rhs) { 
    return lhs.x() < rhs.x() && lhs.y() < rhs.y() && lhs.z() < rhs.z(); 
} 

inline bool cmpVecs(const vec3 &lhs, const vec3 &rhs) { 
    return lhs.x() < rhs.x() && lhs.y() < rhs.y() && lhs.z() < rhs.z(); 
} 


inline void removeDuplicates(std::vector<vec3> &con) 
{ 
    std::sort(con.data(), con.data() + con.size()); 
    auto itr = std::unique(con.begin(), con.end(), cmpVecs); 
    con.resize(itr - con.begin()); 
} 

void init(std::vector<vec3> &verts) { 
    removeDuplicates(verts); 
} 

VS 2012的错误:

algorithm(3618): error C2678: binary '<' : no operator found which takes a left-hand operand of type 'Eigen::Matrix<_Scalar,_Rows,_Cols>' (or there is no acceptable conversion) 1> with 1> [ 1> _Scalar=float, 1> _Rows=3, 1>
_Cols=1 1> ]

相关文章:

+1

你的比较函数是不正确的,因为'std :: sort'需要一个严格的弱排序谓词。 (lhs.x() dalle

+0

以上评论中的比较适用于我。这很难找到,尽管这在matlab中很常见。 –

回答

1

std::sort有可用的替代,它允许你指定是哪一个比较要使用例如:

struct vec3comp{ 
    bool operator()(const vec3 &lhs, const vec3 &rhs){ 
     return lhs.x() < rhs.x() && lhs.y() < rhs.y() && lhs.z() < rhs.z(); 
    } 
} mycomp; 

std::sort(con.data(), con.data() + con.size(),mycomp);` 

编辑:你可以用lambda函数显示你的代码吗?它应该工作正常。

+0

TBH,我不认为我的排序方法不正确。我认为它是按每个浮动而不是按每3分类。我只是复制了我在上面发布的链接。它也可能不是字节对齐的...现在检查。 – ffhighwind

+0

是的,static_assert在测试大小== 3 * sizeof(float)时失败,这在某些方面是合理的,虽然很糟糕。无论哪种方式,它都会给运算符带来错误(即使使用链接的struct方法时,尽管它应该写成mycomp()。 – ffhighwind

+0

nvm我正在做sizeof(sizeof())...所以就像我说的。即使在尝试结构时仍然会给我错误。 – ffhighwind