在c++中std::vector是一个常用的容器,在此记录一下去除该容器内重复元素的操作:
struct linePoint
{
linePoint() {};
linePoint(const double& startX, const double& startY, const double& endX, const double& endY) {
this->startX = startX;
this->startY = startY;
this->endX = endX;
this->endY = endY;
};
bool linePoint::operator==(const linePoint& input)
{
return(this->startX == input.startX && this->startY == input.startY &&
this->endX == input.endX && this->endY == input.endY);
}
public:
double startX, startY, endX, endY;
};
std::vector<linePoint> linePointVec;
for (int i = 0; i < 3;++i) {
linePointVec.emplace_back(10, 100,100, 100);
linePointVec.emplace_back(10, 130,100, 130);
linePointVec.emplace_back(10, 160,100, 160);
}
for (auto &it: linePointVec) {
cout << it.startX << " " << it.startY << " " << it.endX << " " << it.endY << endl;
}
for (auto it = ++linePointVec.begin(); it != linePointVec.end();) {
auto it1 = std::find(linePointVec.begin(), it, *it);
if (it1!=it) {
it = linePointVec.erase(it);
}
else {
it++;
}
}
for (auto& it : linePointVec) {
cout << it.startX << " " << it.startY << " " << it.endX << " " << it.endY << endl;
}
对于自定义的结构体,一定记得重载运算符"=="