c++ - std::map using large amounts of memory -
c++ - std::map using large amounts of memory -
i have array of bins:
vector<vector<int> > bins
the programme calculates big amount of object of type term
, corresponding integer. every bin corresponds term
object, , ints need end in bins. doing this:
map<term, int> helpermap; int binnumber=0; while(/*some condition*/) { term temp; int tempint; //do calculation find temp & tempint if (helpermap.find(temp)==helpermap.end()) { vector<int> newbin; newbin.push_back(tempint); bins.push_back(newbin); helpermap[temp] = binnumber; binnumber++; } else { bins[helpermap.find(temp)->second].push_back(tempint); } }
the problem map (which don't need anymore farther on) requires much more memory bin structure, , putting serious constraints on program. there more efficient way this?
i in 2 steps. spin through loop once, calculate how many bins you're going need, , phone call bins.resize(numberyouwillneed)
- spin through 1 time again , need bins[x].push_back(tempint);
- result in far fewer memory allocations too, making faster. depending on whether calculations expensive , whether can them once.
however, can't see helpermap
beingness enormously bigger bins
- sure problem?
c++ std
Comments
Post a Comment