From: Roy S. <roy...@ic...> - 2012-06-13 19:08:01
|
On Wed, 13 Jun 2012, Kirk, Benjamin (JSC-EG311) wrote: >> On the other hand I was thinking that the sorting feature of the set isn't >> a bad thing because you're going to loop over them calling set() on the >> numeric vector... so having them in order might things more efficient >> (memory access wise)... but I'm sure that doesn't play out in reality and >> I'm just trying to justify my inefficient but simple algorithm ;-) > > Maybe. I like Roy's suggestion though, and importnantly it is also more > memory efficient. True. I think a set<int> takes up about 3 times the space of a vector<int> that's been preallocated to the same size. Up that to 4 times the space if you need to copy the set into a vector anyway to hand off to an API expecting a vector or array. But then cut that down by some significant but unknown factor unless your vector-based user code does two passes, with the first to figure out how much space to reserve(). If you just push_back() then your STL implementation will try to save you a bunch of CPU time at the cost of memory by doing reallocations with increasingly large chunks of spare overhead. --- Roy |