Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I have got a solution from StackOverflow, see the post:

#include <vector>
#include <unordered_set>


struct Point2f
{
    float x;
    float y;
    Point2f(float a, float b) : x(a), y(b) {}
    Point2f() : x(0), y(0) {}
};

bool operator==(const Point2f& pt1, const Point2f& pt2)
{
    return ((pt1.x == pt2.x) && (pt1.y == pt2.y));
}

namespace std
{
    template<>
    struct hash<Point2f>
    {
        size_t operator()(Point2f const& pt) const
        {
            return (size_t)(pt.x*100 + pt.y);
        }
    };
}


void removedupes(std::vector<Point2f> & vec)
{
    std::unordered_set<Point2f> pointset;

    auto itor = vec.begin();
    while (itor != vec.end())
    {
        if (pointset.find(*itor) != pointset.end())
        {
            itor = vec.erase(itor);
        }
        else
        {
            pointset.insert(*itor);
            itor++;
        }
    }
}


int main(int argc, char* argv[])
{
    std::vector<Point2f>  pointTemp;

    pointTemp.resize(6);

    pointTemp[0]=Point2f(1,1);
    pointTemp[1]=Point2f(2,3);
    pointTemp[2]=Point2f(1,1);
    pointTemp[3]=Point2f(2,3);
    pointTemp[4]=Point2f(1,1);
    pointTemp[5]=Point2f(4,1);

    removedupes(pointTemp);

    return 0;
}