I have a std::map<int64_t, int64_t> foo;
.. it is fed daily with XX amount of pairs (not defined, they may be 1 or 1000).
To reduce memory usage, i want to delete no more useful elements of my map.
i did this:
map<int64_t, int64_t> tmpMap;
// Copy into a new temporary map only elements to keep
for (map<int64_t, int64_t>::iterator it = foo.begin(); it != foo.end(); ++it)
{
// the condition decides whether a pair is still useful or not
if ([...])
{
pair<int64_t, int64_t> tmpPair(it->first, it->second);
tmpMap.insert(tmpPair);
}
}
// Clear the main map
foo.clear();
// Copy tmpMap (which contains only useful elements) into the main map
foo.insert(tmpMap.begin(), tmpMap.end());
tmpMap.clear();
Any suggestion on how to reach my goal in a better way in terms of resource usage, considering that foo
may have 500/600 pairs of int64_t and these line are called each time foo
is fed?
Thanks (and sorry for my english)