1

I read a very large number of point coordinates x, y and z and store them in a vector of Point structures to do further processing.

struct Point{
    double x,y,z;
    Point (double x0, double y0, double z0): x(x0), y(y0), z(z0) {}
}


vector<Point> pv;
pv.push_back (Point(x,y,z));

I also have a hash function that maps all the points with the same coordinates together with their vector index to an unordered_multimap for some other processing.

the problem is there are many duplicate points that I read from the file and I have to get rid of those elements. I have seen solutions on how to remove duplicate elements from a vector of primitive types but that does not apply to my case. In addition many of the proposed methods, requires sorting that is not applicable to a Point structure.

Arash
  • 141
  • 10
  • std::remove_if? –  Feb 26 '17 at 16:01
  • In the context of your problem, it's not meaningful to say that one point is less than or greater than another. However, you could still introduce a total ordering onto the points, which would enable you to use the methods that require sorting (e.g. std::unique). For example, you could just sort by the x coordinate first. If two points have equal x coordinates, sort by the y coordinates. If they have equal y coordinates, sort by the z coordinates. If the z coordinates are equal, the points are equal. Depending on how you get the values, the fact that you are using doubles could pose an issue. – IncongruentModulo1 Feb 26 '17 at 16:06

2 Answers2

1

Define the less operator and then apply the same method as for primitive type

How to define the operator is:

struct Point{
    (...)
    const bool operator < ( const Point &r ) const{
        return (x<r.x) || (x==r.x && y<r.y) || (x==r.x && y==r.y && z<r.z);
    }
};
Gabriel
  • 3,564
  • 1
  • 27
  • 49
1

You can use the so-called erase-remove "idiom":

#include <algorithm>
...
pv.erase(std::remove(pv.begin(), pv.end(), pointToRemove), pv.end());

where operator== has been defined for Point:

bool operator==(Point a, Point b) {
    return a.x == b.x && a.y == b.y && a.z == b.z;
}
Community
  • 1
  • 1
Emil Laine
  • 41,598
  • 9
  • 101
  • 157