به این شکل :
#include <vector>
#include <unordered_set>
#include <functional>
#include <iostream>
struct Point
{
float x;
float y;
Point() : x(0), y(0) {}
Point(float a, float b) : x(a), y(b) {}
};
void removedupes(std::vector<Point> & vec)
{
auto hash = [](const Point& pt){
return (size_t)(pt.x*100 + pt.y);
};
auto equal = [](const Point& pt1, const Point& pt2){
return ((pt1.x == pt2.x) && (pt1.y == pt2.y));
};
using equalFunc = std::function<size_t(const Point&,const Point&)>;
using hashFunc = std::function<size_t(const Point&)>;
using PointHash = std::unordered_set<Point,hashFunc,equalFunc>;
PointHash pointset(vec.size(),hash,equal);
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<Point> pointTemp;
pointTemp.resize(6);
pointTemp[0]=Point(1,1);
pointTemp[1]=Point(2,3);
pointTemp[2]=Point(1,1);
pointTemp[3]=Point(2,3);
pointTemp[4]=Point(1,1);
pointTemp[5]=Point(4,1);
removedupes(pointTemp);
for(auto p :pointTemp){
std::cout<<p.x<<" "<<p.y<<'\n';
}
return 0;
}