Question: I am creating some stuff on a `.on()` jquery handler like that

```
$('#idhere').on('click', function(){
   //create a couple of divs with a class
   });

```

Now I also want to add an event on the divs created, I have tried:

```
$('.divcreated').bind('mouseover', function(){
   //do some magic
   });

```

For some reason the second event is never triggered probably because they are both asynchronous. Any ideas to trigger the second event?

Answer: 
Think of an iterator as a pointer to an element in the list. So, instead of making the `calc_average` function act on individual members of `mystruct` you may want to have it take a reference to a `mystruct` object and doing something with that.

For instance:

```
void do_work( mystruct& s )
{
  ++s.count;
}

std::list<mystruct> mylist;

// populate list

std::for_each( mylist.begin(), mylist.end(), do_work );

```

You can also achieve the same effect with range based `for` loops

```
for( auto& elem : mylist ) {
  do_work( elem );
}

```

I would rewrite the `calc_average` function as

```
void calc_average( mystruct& s, long quant )
{
    s.avg = ( (s.avg * s.count) + quant ) / (s.count + 1);
    s.count++;
}

```

To use it with range based `for` loops is trivial, but to use it with `std::for_each` you'll have to use `std::bind` to bind the `quant` parameter.

```
std::for_each( mylist.begin(), mylist.end(), 
  std::bind( calc_average, std::placeholders::_1, qty ) );

```
I am currently working with dynamic content and I found
I have a SPRITE structure
std::list characters\_spr;
I declare a character by SPRITE character then I use

```
characters_spr.push_back(character)

```

then
I use a for loop to iterate over the list

```
for(SPRITE &character : characters_spr){
   somefunction(character, ....);
}

```

in the somefunction I have
somefunction(SPRITE &spr, ......................)

and when the somefunction is called it updates the character data for the character within the list.