Saturday, October 4, 2008

Weekly Code: C# ForEach Method In C++

This week's source code is a C++ version of C#'s ForEach method available here The ForEach method isn't the foreach statement, C#'s ForEach method you pass a delegate to a method and it will execute the method for every element in the collection.

The C++ version works similar where you pass it the vector collection and a function pointer, it will then execute the function passing in the vector. The function can then perform any action on the vector, in this case sample it change the values to 1.

Function pointers are powerful in C++ but yes they've a very error-prone ugly syntax.

No comments: