How do you write your own looping function?

This is a discussion on How do you write your own looping function? within the C++ Programming forums, part of the General Programming Boards category; Its that time again. time for another one of my weird questions. This one is a real gem. Im plotting ...

  1. #1
    0x7f
    Guest

    Question How do you write your own looping function?

    Its that time again. time for another one of my weird questions. This one is a real gem. Im plotting to write my own foreach function (ya im a perl nut, tuff.) however writing a function that takes code i guess in {}'s isnt exactly standard material for C++ books to cover. Im not even sure what to call this type of function. the end result im aiming for is something like:

    foreach(T bob, vector<T>) { dothis(with bob); }

    my thoughts were templating as much as i can so it could take arrays or vectors or possibly other STL containers. am i completely nutty in wanting to write my own looping function? or is this even do-able? oh well, im dieing to hear what you guys have to say.

    me.

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    The STL already offers a for_each function
    Code:
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    void print(int);
    
    int main()
    {
        int array[9] = {1,2,3,4,5,6,7,8,9};
    
        for_each(array, array + 9, print);
    }
    
    void print(int num)
    {
        cout<< num <<' '<<flush;
    }

  3. #3
    0x7f
    Guest

    Angry =(

    aww your taking all the fun out of it, the entire point of writing it is to figure it out HOW to write it, not to auctually use it for anything. =P

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    For looping in general, take a look at the tutorials (lesson 3).

  5. #5
    0x7f
    Guest
    not USING loops, im talking about writing a function that IS a loop.

  6. #6
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    not USING loops, im talking about writing a function that IS a loop.
    But the function that IS a loop, uses a loop. The definition of for_each is this
    Code:
    template <typename In, typename Op>
    Op for_each(In first, In last, Op f)
    {
        while (first != last)
            f(*first++);
    
        return f;
    }
    The only way to create a loop without using while, for, or do/while is to fake it with goto or recursion. Even Perl's foreach is implemented with a loop, check the source code if you don't believe me.

  7. #7
    0x7f
    Guest
    im aware of that, and i never thought i would go without using a while loop in it, however my question centers on how do you pass what you want your function to do? I have no clue how to pass whats in the {}'s. The loop needs something to do, and thats what im really looking for.

  8. #8
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    I have no clue how to pass whats in the {}'s. The loop needs something to do, and thats what im really looking for.
    How about a place to start, a place to end, and the operation to perform on that sequence?

  9. #9
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Hahaha.
    If you ever need a hug, just ask.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21