Re: basic static variables question
On Mon, 19 Nov 2007 01:03:29 -0800 (PST), pauldepstein@att.net wrote:
On Nov 19, 4:55 pm, Sohail Somani <soh...@taggedtype.net> wrote:
On Mon, 19 Nov 2007 00:44:46 -0800, pauldepstein wrote:
I have code that looks like this:
for (int counter = 0; counter < 100; ++ counter) {
{ static int blah = 0;
if (blah == 0)
// do something;
blah ++;
}
}
This is already legal code. My problem is that I want it to execute the
inner loop 100 times.
There is no inner loop. Only a single loop. Is that what you meant to
post?
Thanks a lot for following up. The code posted is as intended. There
is one block ( hence the { } ) inside the for loop. I was
carelessly calling the inner block a loop. I want it to do what's in
the inner block exactly once for each value of the counter.
You mean, when you issue the loop multiple times, it will invoke
the particular code only once for each distinct value of the loop?
I.e.
for(int c=0; c<10; ++c)
for(int a=0; a<10; ++a)
for(int b=5; b < 5+a; ++b)
{
if(something here)
std::cout << b << ',';
}
This would output 5,6,7,8,9,10,11,12,13, and nothing else?
You can try something like this:
for(int c=0; c<10; ++c)
for(int a=0; a<10; ++a)
for(int b=5; b < 5+a; ++b)
{
static std::set<int> seen;
if(seen.find(b) == seen.end())
{
seen.insert(b);
std::cout << b << ',';
}
}
Instead of std::set<>, you can use some other data structure
that fits your purpose better if you are so inclined.
--
Joel Yliluoma - http://iki.fi/bisqwit/