基本的静态变量问题 [英] basic static variables question
问题描述
我的代码如下:
for(int counter = 0; counter< 100; ++ counter)
{
{static int blah = 0;
if(blah == 0)
//做点什么;
blah ++;
}
}
这已经是合法代码了。我的问题是我希望它执行内循环100次的
。每次递增计数器时,我都希望它只需要内部循环一次 - 对于计数器的每个值一次。
所以我把blah变量静态强制执行这个曾经只有的房产
(强制执行某项工作,每个价值从未超过一次
的柜台)。但是现在我预计它只会在
总计中做一次,而不是100多岁的事情 - 一次对于柜台的每个价值。
任何解决方案必须是标准问题。
非常感谢你的帮助,
Paul Epstein
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. Each time it increments counter, I want it
to do the inner loop exactly once -- once for each value of counter.
So I made the blah variable static to enforce this once-only property
(to enforce that do something was never done more than once per value
of counter). But now I expect it will only do something once in
total, not 100 somethings -- once for each value of the counter.
Any solution to what must be a standard problem.
Thank you very much for your help,
Paul Epstein
推荐答案
2007年11月19日星期一00:44:46 -0800,pauldepstein写道:
On Mon, 19 Nov 2007 00:44:46 -0800, pauldepstein wrote:
我有代码看起来像这样:
for(int counter = 0; counter< 100; ++ counter){
{static int blah = 0;
if(blah == 0)
//做点什么;
blah ++;
} < br $>
}
这已经是合法代码了。我的问题是我希望它执行
内循环100次。
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.
没有内循环。只有一个循环。这是你的意思
发布?
-
Sohail Somani
http://uint32t.blogspot.com
11月19,4:55 pm,Sohail Somani< soh ... @ taggedtype.netwrote:
On Nov 19, 4:55 pm, Sohail Somani <soh...@taggedtype.netwrote:
2007年11月19日星期一00:44:46 -0800, pauldepstein写道:
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)
//做某事;
blah ++;
}
for (int counter = 0; counter < 100; ++ counter) {
{ static int blah = 0;
if (blah == 0)
// do something;
blah ++;
}
}
}
这已经是合法代码。我的问题是我希望它执行
内循环100次。
This is already legal code. My problem is that I want it to execute the
inner loop 100 times.
没有内循环。只有一个循环。这是你的意思
发布?
-
Sohail Somanihttp://uint32t.blogspot.com
There is no inner loop. Only a single loop. Is that what you meant to
post?
--
Sohail Somanihttp://uint32t.blogspot.com
Sohail,
非常感谢您的跟进。发布的代码符合预期。
是for循环中的一个块(因此是{})。我是
不小心将内部块调用为循环。我希望它对于计数器的每个值只对内部块执行一次'
。
再次感谢,
Paul Epstein
Sohail,
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.
Thanks again,
Paul Epstein
pa * *********@att.net 写道:
pa**********@att.net wrote:
11月19日下午4:55,Sohail Somani< soh。 .. @ taggedtype.netwrote:
On Nov 19, 4:55 pm, Sohail Somani <soh...@taggedtype.netwrote:
> 2007年11月19日星期一00:44:46 -0800,pauldepstein写道:
>On Mon, 19 Nov 2007 00:44:46 -0800, pauldepstein wrote:
我的代码如下所示:
I have code that looks like this:
for(int counter = 0; counter< 100; ++柜台){
{static int blah = 0;
if(blah == 0)
//做点什么;
blah ++;
}
for (int counter = 0; counter < 100; ++ counter) {
{ static int blah = 0;
if (blah == 0)
// do something;
blah ++;
}
}
}
这已经是合法代码。我的问题是我希望它执行内循环100次的
。
This is already legal code. My problem is that I want it to execute
the inner loop 100 times.
没有内循环。只有一个循环。这是你的意思吗?
-
Sohail Somanihttp://uint32t.blogspot.com
There is no inner loop. Only a single loop. Is that what you meant to
post?
--
Sohail Somanihttp://uint32t.blogspot.com
Sohail,
非常感谢您的跟进。发布的代码符合预期。
是for循环中的一个块(因此是{})。我是
不小心将内部块调用为循环。我希望它对于计数器的每个值只对内部块执行一次'
。
Sohail,
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.
我仍然没有关注。为什么会这样($ counter = 0;计数器< 100; ++计数器)
{
// do do
一些东西;
}
不对计数器的每个值只执行一次块吗?
Best
Kai-Uwe Bux
I am still not following. Why would
for (int counter = 0; counter < 100; ++ counter)
{
//do something;
}
not execute the block exactly once for each value of the counter?
Best
Kai-Uwe Bux
这篇关于基本的静态变量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!