473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Getting current iteration in a loop

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<intl;
// ...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.
Jan 3 '07 #1
3 1310
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<intl;
// ...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
Jan 3 '07 #2
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<intl;
// ...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

Jan 3 '07 #3
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
5
by: Florian Lindner | last post by:
Hello, when I'm iterating through a list with: for x in list: how can I get the number of the current iteration? Thx, Florian
0
by: Danny Anderson | last post by:
Hola, C++ folk! I want to have a do...while loop that works with a different file each iteration, prompting the user for the file name to open and write. The problem I am having is that the...
2
by: Bruce Whitehouse | last post by:
This should be an easy one, but I'm not sure the best way to do it. I've got a form, and when I click a button it starts looping through an array. With each iteration of the array I want it to...
23
by: Mitchell Vincent | last post by:
Is there any way to "skip" iterations in a for loop? Example : for x = 1 to 10 if something = 1 next endif
2
by: Mike Mac | last post by:
Hello all, I have a loop and would like to create a new textbox on each iteration of the loop. The code below only generates one textbox no matter how many times the loop loops! Any insight...
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
2
by: Al Reid | last post by:
I'm using VB 2005. I have a production application where I load a ListView with information from several sources based on user interaction. At some point I need to iterate through the Items...
1
by: greyseal96 | last post by:
Hi, I am a pretty new programmer, so I apologize in andvance if this is a dumb question... In a book that I'm reading to learn C#, it says that when using a foreach() loop, a read-only copy of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.