Getting current iteration in a loop

Discussion in 'C++' started by jalina, Jan 3, 2007.

  1. jalina

    jalina Guest

    Is there a way to get the current iteration in a for_each loop ?

    For example:

    void f(int val)
    {
    // Here how get the current
    // index or iterator that get called
    // by the for_each loop ?
    }

    void g()
    {
    list<int> l;
    // ...put some value in l
    for_each(l.begin(), l.end(), f);
    }


    I guess f() has no way to know that is being called in a for_each. So
    should I stick to a basic loop to do that or is there a trick ?


    Tnx,
    J.
    jalina, Jan 3, 2007
    #1
    1. Advertising

  2. jalina wrote:

    > Is there a way to get the current iteration in a for_each loop ?
    >
    > For example:
    >
    > void f(int val)
    > {
    > // Here how get the current
    > // index or iterator that get called
    > // by the for_each loop ?
    > }
    >
    > void g()
    > {
    > list<int> l;
    > // ...put some value in l
    > for_each(l.begin(), l.end(), f);
    > }
    >
    > I guess f() has no way to know that is being called in a for_each. So
    > should I stick to a basic loop to do that or is there a trick ?


    The trick is to write code that does what you want. A function has no state,
    but you can use a functor.

    For example:

    class f
    {
    public:
    f () : count (0)
    { }
    unsigned int count;
    void operator () (int val)
    {
    // Whatever
    ++count;
    }
    };

    for_each (l.begin (), l.end (), f () );

    --
    Salu2
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jan 3, 2007
    #2
    1. Advertising

  3. jalina

    Alan Johnson Guest

    Julián Albo wrote:
    > jalina wrote:
    >
    > > Is there a way to get the current iteration in a for_each loop ?
    > >
    > > For example:
    > >
    > > void f(int val)
    > > {
    > > // Here how get the current
    > > // index or iterator that get called
    > > // by the for_each loop ?
    > > }
    > >
    > > void g()
    > > {
    > > list<int> l;
    > > // ...put some value in l
    > > for_each(l.begin(), l.end(), f);
    > > }
    > >
    > > I guess f() has no way to know that is being called in a for_each. So
    > > should I stick to a basic loop to do that or is there a trick ?

    >
    > The trick is to write code that does what you want. A function has no state,
    > but you can use a functor.
    >
    > For example:
    >
    > class f
    > {
    > public:
    > f () : count (0)
    > { }
    > unsigned int count;
    > void operator () (int val)
    > {
    > // Whatever
    > ++count;
    > }
    > };
    >
    > for_each (l.begin (), l.end (), f () );
    >


    Is this guaranteed to work? for_each takes its functor by value,
    meaning at least one copy is made. Does it guarantee it won't make any
    more copies? For example, would the following be a valid
    implementation of for_each?

    template <class InputIterator, class Function>
    Function for_each(InputIterator first, InputIterator last, Function f)
    {
    for (; first != last; ++first)
    {
    Function g(f) ;
    g(*first) ;
    }
    return f ;
    }

    --
    Alan Johnson
    Alan Johnson, Jan 3, 2007
    #3
  4. Alan Johnson wrote:

    >> class f
    >> {
    >> public:
    >> f () : count (0)
    >> { }
    >> unsigned int count;
    >> void operator () (int val)
    >> {
    >> // Whatever
    >> ++count;
    >> }
    >> };
    >> for_each (l.begin (), l.end (), f () );


    > Is this guaranteed to work? for_each takes its functor by value,
    > meaning at least one copy is made. Does it guarantee it won't make any
    > more copies?


    I don't looked at the standard definition, but for example TC++PL has
    several examples where the state of a functor used with for_each is
    expected to be preserved and available in his returned result. See 18.4 for
    example (but check the errata list first).

    --
    Salu2
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jan 3, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dwerdna
    Replies:
    5
    Views:
    5,319
    Ajeetha
    Mar 31, 2005
  2. Rudi
    Replies:
    5
    Views:
    4,963
  3. Simon Brooke
    Replies:
    20
    Views:
    628
  4. Nene
    Replies:
    6
    Views:
    341
    John W. Krahn
    Dec 13, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    350
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page