C ++ Comparator不工作

时间:2014-01-03 12:34:30

标签: c++ opensg

我正试图通过一组中的x坐标对一组OpenSG点进行排序,但它似乎没有用。

我的比较器:

struct less_than_x {
    inline bool operator() (const OSG::Pnt3f& p1, const OSG::Pnt3f& p2) {
        return (p1.x() < p2.x());
    }
};

我的代码:

std::set<OSG::Pnt3f, less_than_x> positions3D; 

OSG::GeoPnt3fPropertyRefPtr pos = OSG::GeoPnt3fProperty::create();

pos->addValue(OSG::Pnt3f(1,2,3));
pos->addValue(OSG::Pnt3f(5,2,1));
pos->addValue(OSG::Pnt3f(4,4,2));
pos->addValue(OSG::Pnt3f(6,4,0));
pos->addValue(OSG::Pnt3f(3,5,1));

// Remove the Z Axis from all of the points
for(OSG::UInt32 i = 0; i < pos->size(); i++)    {        
    OSG::Pnt3f p;              
    pos->getValue(p,i);
    OSG::Pnt3f p2 = OSG::Pnt3f(p.x(), p.y(), 0);
    positions3D.insert(p2);
}

for (std::set<OSG::Pnt3f>::iterator it = positions3D.begin(); it != positions3D.end(); it++) {
    std::cout << *it << std::endl;
}

当打印出一组点时,它们仍然没有按顺序排列。

非常感谢任何帮助。

编辑:

对于我来说,反之亦然,但是当把它改为双倍并且使用这组点时它会失败。

positions3D.insert(Pnt3f(-1, 0, 0));
positions3D.insert(Pnt3f(-0.850651, -0.309017, 0));
positions3D.insert(Pnt3f(-0.850651, 0, 0));
positions3D.insert(Pnt3f(-0.525731, 0, 0));
positions3D.insert(Pnt3f(-0.525731, 0.809017, 0));
positions3D.insert(Pnt3f(-0.525731, 0.5, 0));
positions3D.insert(Pnt3f(-0.447214, 0.525731, 0));
positions3D.insert(Pnt3f(-0.447214, 0.850651, 0));
positions3D.insert(Pnt3f(-0.447214, 0, 0));
positions3D.insert(Pnt3f(-1.05104e-007, 0.309017, 0));
positions3D.insert(Pnt3f(-7.00695e-008, 0.809017, 0));
positions3D.insert(Pnt3f(0, 1, 0));
positions3D.insert(Pnt3f(7.00695e-008, 0.809017, 0));
positions3D.insert(Pnt3f(1.05104e-007, -0.309017, 0));
positions3D.insert(Pnt3f(0.447214, 0, 0));
positions3D.insert(Pnt3f(0.447214, 0.850651, 0));
positions3D.insert(Pnt3f(0.447214, 0.525731, 0));
positions3D.insert(Pnt3f(0.525731, 0.5, 0));
positions3D.insert(Pnt3f(0.525731, 0.809017, 0));
positions3D.insert(Pnt3f(0.525731, 0, 0));
positions3D.insert(Pnt3f(0.850651, 0, 0));
positions3D.insert(Pnt3f(0.850651, 0.5, 0));
positions3D.insert(Pnt3f(1, 0, 0));

这些按此顺序排序:

-1, 0, 0
-0.850651, -0.309017, 0
-0.850651, 0, 0
-0.525731, 0, 0
-0.525731, 0.809017, 0
-0.525731, 0.5, 0
-0.447214, 0.525731, 0
-0.447214, 0.850651, 0
-0.447214, 0, 0
-1.05104e-007, 0.309017, 0
-7.00695e-008, 0.809017, 0
0, 1, 0
7.00695e-008, 0.809017, 0
1.05104e-007, -0.309017, 0
0.447214, 0, 0
0.447214, 0.850651, 0
0.447214, 0.525731, 0
0.525731, 0.5, 0
0.525731, 0.809017, 0
0.525731, 0, 0
0.850651, 0, 0
0.850651, 0.5, 0
1, 0, 0    

1 个答案:

答案 0 :(得分:1)

请检查以下代码:

#include <set>
#include <iostream>

struct Pnt3f {
        Pnt3f(int x, int y, int z) : m_x(x), m_y(y), m_z(z) {}
        int x() const { return m_x; }
        int y() const { return m_y; }
        int z() const { return m_z; }
private:
        int m_x;
        int m_y;
        int m_z;
};

struct less_than_x {
    inline bool operator() (const Pnt3f& p1, const Pnt3f& p2) {
        return (p1.x() < p2.x());
    }
};

int main() {

        std::set<Pnt3f, less_than_x> positions3D;

        positions3D.insert(Pnt3f(1,2,0));
        positions3D.insert(Pnt3f(5,2,0));
        positions3D.insert(Pnt3f(4,4,0));
        positions3D.insert(Pnt3f(6,4,0));
        positions3D.insert(Pnt3f(3,5,0));

        for (std::set<Pnt3f>::iterator it = positions3D.begin(); it != positions3D.end(); ++it) {
                std::cout << "(" << it->x() << ", " << it->y() << ", " << it->z() << ")" << std::endl;
        }
        return 0;
}

输出如下:

./a.out 
(1, 2, 0)
(3, 5, 0)
(4, 4, 0)
(5, 2, 0)
(6, 4, 0)

因此,比较器按预期工作并进行排序。您需要在行之前打印出值以了解出现了什么问题。

positions3D.insert(p2);