-1

I want to create a function like a for loop where I will ideally enter a variable for the times the iteration should happen and some functions inside brackets my function will execute. I hope I was clear enough.

Example

superFor (1)
{
    // Commands to be executed here  
    add(1 + 2);
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
SMeyers
  • 768
  • 3
  • 9
  • 20
  • 2
    That's a pretty strange request, why not just write a for loop? I'd say a macro, but I want to understand why you want to do this first. – Tom Ritter Oct 17 '08 at 14:46
  • Smells like homework... can you show us what you have tried so far? – freespace Oct 17 '08 at 14:47
  • What do you mean by "some functions inside brackets my function" – Justsalt Oct 17 '08 at 14:57
  • I think the question asker is coming from another language where a for() construct is like a for-each() type of construct, so this is just a simple question of syntax. – Keith Twombley Oct 17 '08 at 15:01
  • Do you mean that you want to pass how many times to run AND what functions to run into the for loop? – Howler Oct 17 '08 at 15:02

7 Answers7

10
#define superFor(n) for(int i = 0; i < (n); i++)

Edit: Be careful to not use another variable called i in the loop.

user9282
  • 680
  • 1
  • 7
  • 15
6

What you want isn't possible in C++ because the (current version of the) language lacks some features that are required here: namely, creating function blocks “on the fly”.

The best you can do is pass a function pointer or function object to your function. The STL offers many examples of this. Consider:

void out(int x) {
    cout << x << ' ';
}

vector<int> xs;
xs.push_back(42);
xs.push_back(23);

for_each(xs.begin(), xs.end(), out);

This passes a pointer to function out to the function for_each.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
3

in C++ you do that with a regular for-loop.

  for(variable; condition; increment) {
    //stuff goes here
  }

In your for-loop: variable is a counting variable like i. You can define the variable right here and initialize it. You often see something like "int i = 0"

condition is some sort of test. In your case you want to check if your counting variable is less than how many times you want the loop to execute. You'd put something like "i < how_many_times_to_loop"

increment is a command to increment the counting variable. In your case you want "i++" which is a short hand way of saying "i = i + 1"

So that gives us:

  for(int i = 0; i < how_many_times_to_loop; i++) {
    //stuff goes here
  }
Keith Twombley
  • 1,666
  • 1
  • 17
  • 21
2

It's crazy, but it might just work...

http://www.boost.org/doc/libs/1_36_0/doc/html/lambda.html.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
1

You could use a macro.

#define superFor(v, i) for (int v = 0; v < (i); v++)

and use it like this:

superFor(i, 10) {
   printf("Doing something ten times");
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Hafthor
  • 16,358
  • 9
  • 56
  • 65
0
void DoSomethingRepeatedly(int numTimesTo Loop)
{
   for(int i = 0; i < numTimesToLoop; i++)
   {  
       // Do whatever; 
   }
}

That it? That can't be.. too simple.. I must be misunderstanding your question :( Of course you'd have to check that the value of numTimesToLoop is >= 0.

Edit: I made it a method, just in case.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
FOR
  • 4,260
  • 2
  • 25
  • 36
0

Maybe BOOST_FOREACH will do what you want:

http://engineering.meta-comm.com/resources/cs-win32_metacomm/doc/html/foreach.html

Scott Langham
  • 58,735
  • 39
  • 131
  • 204