This is my first time using c++ and I am having trouble manipulating a set. The function is supposed to iterate through the set, find the pair of Regions* that have the least distance between them, insert a new Region* and remove the two selected Regions* from the list. However, when I call s.insert() the set will be updated, but when the outer while loops begins again the set size is still the original. I commented out the s.erase() lines, but I was having the same issue. I have tried creating a new set and working off the one that is passed. Any help would be greatly appreciated. I have a feeling it has something to with pointers.
Region* reduce(set<Region*>& ls) {
set<Region*> s = ls;
int i = 0;
std::set<Region*> remove;
while (i < 5) {
double cur_smallest = 0.0;
Region* smallest1 = new Region(0, 0, 0, 0);
Region* smallest2 = new Region(0, 0, 0, 0);
std::set<Region*>::iterator rIterator;
std::set<Region*>::iterator regionsIterator = s.begin();
while (regionsIterator != s.end()) {
Region* current = *regionsIterator;
if (s.size() == 1) {
return current;
}
rIterator = s.begin();
while (rIterator != s.end()) {
Region* c = *rIterator;
double d = c->distance(*current);
if (d < cur_smallest) {
cur_smallest = d;
smallest1 = c;
smallest2 = current;
}
else if (d == cur_smallest) {
if ((getArea(c) + getArea(current)) <= (getArea(smallest1) + getArea(smallest2))) {
smallest1 = c;
smallest2 = current;
}
}
rIterator++;
}
regionsIterator++;
}
Region tmp = *(smallest1,smallest2);
s.insert(&tmp);
//s.erase(smallest1);
//s.erase(smallest2);
cout << s.size();
i++;
}