• Create Account

### #ActualSiCrane

Posted 27 November 2012 - 05:13 PM

One thing that puzzles me is the operator input within the parameters. I'm not entirely sure how to supply that parameter input or what it does. If I had to guess its something to do with iterating...

As written you can supply anything function like that would take a reference to a const value_type including function pointers, function objects, lambdas, etc. If you don't understand it, you can just replace the call with the actual functionality you want to do on each element.

I noticed you left out the 3rd parameter in the for loop and incremented it within the loop below the 'operator'. Looks intriguing. Could you explain this?

There's not much to explain. All three parts that go inside the parenthesis for a for loop are optional. for (A; B; C) is just syntactic sugar for
{ A;
while (B) {
// body of for loop
C;
}
}

With the exception that if you leave out the second part the while loop looks like while (true). If you leave off the first part, nothing is done before the going through the loop for the first time. If you leave out third part, nothing extra happens after the body of the for loop. So for (;;) {} would be the same as while (true) {}. Since in this loop what happens to itr is different depending on what itr refers to, trying to stick it in the third part of the for loop, while possible, would be horribly unreadable.

Also, I see you made map a const in the parameters. Was there a reason for that? I'm likely going to be changing some object within the loop if that's advisable to do.

This code doesn't handle the situation of adding or removing nodes from the map while it's looping. Ex: if for some reason you remove the element pointed to by last, it'll keep looping until it crashes. However, one of the interesting things about std::map is that if you have a map with pointers as the values, you can call non-const functions on the pointed to objects from a const_iterator. That is to say, this is legal:
  std::map<int, Foo *> foo_map;
// fill the map
std::map<int, Foo *>::const_iterator itr = foo_map.begin();
itr->second->non_const_function();


: formatting

### #1SiCrane

Posted 27 November 2012 - 05:12 PM

One thing that puzzles me is the operator input within the parameters. I'm not entirely sure how to supply that parameter input or what it does. If I had to guess its something to do with iterating...

As written you can supply anything function like that would take a reference to a const value_type including function pointers, function objects, lambdas, etc. If you don't understand it, you can just replace the call with the actual functionality you want to do on each element.

I noticed you left out the 3rd parameter in the for loop and incremented it within the loop below the 'operator'. Looks intriguing. Could you explain this?

There's not much to explain. All three parts that go inside the parenthesis for a for loop are optional. for (A; B; C) is just syntactic sugar for
{ A;
while (B) {
// body of for loop
C;
}
}

With the exception that if you leave out the second part the while loop looks like while true. If you leave off the first part, nothing is done before the going through the loop for the first time. If you leave out third part, nothing extra happens after the body of the for loop. So for (;;) {} would be the same as while (true) {}. Since in this loop what happens to itr is different depending on what itr refers to, trying to stick it in the third part of the for loop, while possible, would be horribly unreadable.

Also, I see you made map a const in the parameters. Was there a reason for that? I'm likely going to be changing some object within the loop if that's advisable to do.
This code doesn't handle the situation of adding or removing nodes from the map while it's looping. Ex: if for some reason you remove the element pointed to by last, it'll keep looping until it crashes. However, one of the interesting things about std::map is that if you have a map with pointers as the values, you can call non-const functions on the pointed to objects from a const_iterator. That is to say, this is legal:
  std::map<int, Foo *> foo_map;
// fill the map
std::map<int, Foo *>::const_iterator itr = foo_map.begin();
itr->second->non_const_function();


PARTNERS