I would like to check all Elements of an vector against each other. By checking a condition an element should be removed.
One approach was to erase the elements by nested for loops
for (int a = 0; a < rs.size(); a++)
{
Point A = rs[a];
for (int b = 1; b <= rs.size(); b++)
{
Point B = rs2[b];
float distance = sqrt(pow(B.x - A.x, 2) + pow(B.y - A.y, 2) * 1.0);
if (distance < 10.0)
{
if (distance > 0)
{
rs.erase(rs.begin() + b);
}
}
}
}
but this would effect the vector and his size in runtime.
A second approach was to collect the index of b in an unordered_set but how can I delete the elements with the correspondig index in the original vector?
unordered_set<int> index;
for (int a = 0; a < rs.size(); a++)
{
Point A = rs[a];
for (int b = 0; b < rs.size(); b++)
{
Point B = rs2[b];
float distance = sqrt(pow(B.x - A.x, 2) + pow(B.y - A.y, 2) * 1.0);
if (distance < 10.0)
{
if (distance > 0)
{
index.insert(b);
}
}
}
}
As you might expect, this approach does not work either:
for (const int& idx : index)
{
rs.erase(rs.begin() + idx);
}
Any help?