Kitty wrote:
vector<Obj> v;
....
for(i=0;i<v.size();i++)
v[i].some_data=0;
Unless the "..." indicates something which fills the vector with
objects, the above loop, of course, does nothing. Assuming that
the "..." actually fill the 'std::vector<...>', why doesn't it
immediately set up appropriate values for the member 'some_data'?
Have you measured that this loop is actually a bottleneck?
That said, note that use of 'std::fill()' does not directly work
because you just want to set one member while 'std::fill()'
assumes that you set the complete object. An alternative would
be use of a property map based implementation of the standard
algorithms or an appropiate iterator wrapper. The algorithm may
take advantage of various optimizations like loop-unrolling a la
Duff's Device:
| std::vector<Obj>::iterator beg = v.begin(), it = v.end();
| switch (v.size() % 4)
| do
| {
| (--it)->some_data = 0;
| case 3: (--it)->some_data = 0;
| case 2: (--it)->some_data = 0;
| case 1: (--it)->some_data = 0;
| }
| while (beg != it);
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting