Foreach for Int Array





9
Date Submitted Mon. Oct. 9th, 2006 6:57 AM
Revision 1 of 1
Helper shell
Tags C | CPlusPlus
Comments 6 comments
I was coding a project that used arrays when the lack of a standard foreach in C/C++ got to me. So, I coded this little function to help me. It applies a function that returns int to each element of the array.

void foreach(int a[], int func(int n))
{
        for(int i = (sizeof(a) / 4) - 1; i > -1; --i)
                a[i] = func(a[i]);
}
 

#include <iostream.h>

int myprint(int i)
{
        cout << i << endl;
        return i;
}

int add1(int i)
{
        return i + 1;
}

void main()
{
        int arr = {0, 1, 2, 3};
        foreach(arr, myprint);
        foreach(arr, add1);
        foreach(arr, myprint);
}
 

Comments

Comments Elegant
Mon. Oct. 9th, 2006 8:58 AM    Scripter sehrgut
Comments Not bad, but...
Mon. Oct. 9th, 2006 10:46 AM    Helper jarfil
Comments Nice
Mon. Oct. 9th, 2006 7:07 AM    Newbie Shigun
Comments Superfluous and bad
Tue. Oct. 10th, 2006 7:08 AM    Newbie bullestock
  Comments Are you serious?
Wed. Oct. 11th, 2006 2:56 PM    Helper shell
    Comments worste than worthless
Fri. Oct. 20th, 2006 4:41 AM    Newbie tster

Voting