Ask Your Question
0

how to delete repeating coordinates of vector<Point2f>

asked 2014-08-08 03:35:57 -0600

justrookie gravatar image

I passed coordinates of points into vector, and there are some repeating points, so I want to delete other repeating points and just keep the only points.

for example:

vector<Point2f>  points;

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

I want to get the result like this:

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

PS The order of remaining elements is unchanged.

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
1

answered 2014-08-08 05:53:57 -0600

thdrksdfthmn gravatar image

updated 2014-08-08 09:46:21 -0600

Use a std::set. Push every point and duplicates are not kept.

... but, yes, the order is not kept...

Maybe you can create the container that you need using Boost.Multiindex, see example

edit flag offensive delete link more

Comments

1

But there is no guarantee that the order is preserved!

FooBar gravatar imageFooBar ( 2014-08-08 09:24:32 -0600 )edit

try std::unordered_set

konstunn gravatar imagekonstunn ( 2014-08-08 21:41:00 -0600 )edit
1

answered 2014-08-10 08:31:24 -0600

justrookie gravatar image

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;
}
edit flag offensive delete link more
-2

answered 2014-08-08 05:18:00 -0600

gfx gravatar image

updated 2014-08-08 05:22:46 -0600

you need really these????

if ((point[0].x != yourmemorypoint.x) & (point[0].y != yourmemorypoint.y))
{yourmemorypoint.x = point[0].x;
yourmemorypoint.y = point[0].y;}
else { .... cancel point ...

in a FOR cycle (... absolutely not tested ....)

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-08-08 03:35:57 -0600

Seen: 2,674 times

Last updated: Aug 17 '14