454,137 Members | 1,024 Online Need help? Post your question and get tips & solutions from a community of 454,137 IT Pros & Developers. It's quick & easy.

# Getting current iteration in a loop

 P: n/a 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
3 Replies

 P: n/a 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

 P: n/a 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 Function for_each(InputIterator first, InputIterator last, Function f) { for (; first != last; ++first) { Function g(f) ; g(*first) ; } return f ; } -- Alan Johnson Jan 3 '07 #3

 P: n/a 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 Jan 3 '07 #4

### This discussion thread is closed

Replies have been disabled for this discussion. 