c++ - Why don't the changes to my variables survive until the next iteration? -
i want update instances of struct
storing in map
within loop, changes instance variables don't survive iterations of loop (within 1 iteration, new variables set, in next operation reset initial value).
here simplified version of doing:
map<int, regionoverframes>* foundregions = new map<int, regionoverframes>; (int = 0; < frames.size(); i++) { // find regions in current frame map<int, regionoverframes> regionsincurrentframe; (region region: currentframe.regions) { if (foundregions->count(region.regionid) == 0) { regionoverframes foundregion; foundregion.regionid = region.regionid; regionsincurrentframe[region.regionid] = foundregion; (*foundregions)[region.regionid] = foundregion; } else if (foundregions->count(region.regionid) > 0) { regionoverframes foundregion = (*foundregions)[region.regionid]; regionsincurrentframe[region.regionid] = foundregion; } } // update found regions (either adding weight or setting end index) (auto = foundregions->begin(); != foundregions->end(); it++) { regionoverframes foundregion = it->second; // region found before present in frame if (regionsincurrentframe.count(foundregion.regionid) > 0) { float weight = currentframe.getregion(foundregion.regionid).getweight(); foundregion.accumulatedweight += weight; // update of accumulatedweight not present in next loop, accumulatedweight gets assigned value of weight here, in next iteration it's reset 0 } } }
could have fact using iterator it
access objects within map<int, regionoverframes>* foundregions
or fact foundregions
declared pointer , stored on heap?
note regionoverframes
simple struct
looking this:
struct regionoverframes { int regionid; double accumulatedweight; }
your issue creating copy of found region rather updating object found in map.
regionoverframes foundregion = it->second; // ^ copy created
you should use references instead:
regionoverframes &foundregion = it->second; // ^ use reference
Comments
Post a Comment