C++ 计数器和累加器有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12983063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between a counter and an accumulator?
提问by user1756913
I'm not sure what the difference are, but here are the problems.
我不确定有什么区别,但这里是问题所在。
Write an assignment statement that updates a counter variable named numStudents by 1.would it be:
编写一个赋值语句,将名为 numStudents 的计数器变量更新 1。会是:
numStudents = numStudents + 1
numStudents = numStudents + 1
? My other problem is
? 我的另一个问题是
Write an assignment statement that updates an accumulator variable named total by the value in the sales variable.would it be identical like:
编写一个赋值语句,通过 sales 变量中的值更新名为 total 的累加器变量。它会是一样的吗:
total = total + sales
总计 = 总计 + 销售额
?
?
回答by Alberto Moriconi
There isn't a single, clear, univocal answer to your question.
你的问题没有一个单一的、明确的、明确的答案。
It may be said that a counteris a variable that is incremented every time a given event is verified (e.g. when iterating an array, every time you encounter the number 5, you could increment a counter by 1).
可以说,计数器是每次验证给定事件时递增的变量(例如,在迭代数组时,每次遇到数字 5 时,都可以将计数器递增 1)。
The notion may be generalized because there are counters that are incremented by 2, or 3, or any value you'd like, on every step; however, the semantics of a counter usually lose sense if the stepisn't always the same. It depends on the logic of your program, in the end: if you're keeping track of single and double rooms in an hotel, your numGuests
counter could be incremented with steps of 1
and 2
depending on the rooms you're processing at a given moment, however it could be argued that in the end this is just for clarity or brevity, because the result is the same as incrementing it by 1
, twice!
这个概念可能是通用的,因为每一步都有计数器增加 2 或 3,或者任何你想要的值;然而,如果步骤不总是相同的,计数器的语义通常会失去意义。这取决于你的程序的逻辑,到底:如果你正在跟踪的单,双人房在酒店,你的numGuests
计数器可以与步骤递增1
,并2
根据房间你处理在给定时刻,然而,可以争论的是,这最终只是为了清晰或简洁,因为结果与将其增加1
, 两次相同!
An accumulatoris, instead, a variable that, for example, stores the sum of the elements of the array (i.e. you have not a fixed step, but the increment varies based on the elements you encounter).
一个累加器是,相反,即,例如,存储阵列的元素的总和(即,还没有一个固定的步骤,但增量变化的基础上遇到的元素)的变量。
The notion may be generalized for a list of items, applying a given function f
repeatedly to the actual value of the accumulator and the next item of the list, saving the result in our accumulator. In this way, we have obtained the semantics of the foldhigh-order function(one of its other names is in fact accumulate). If we limit our analysis to what is called left fold, the accumulator stores at every moment an intermediate result, valid for the subset you already processed.
这个概念可以推广到项目列表,将给定的函数f
重复应用于累加器的实际值和列表的下一个项目,将结果保存在我们的累加器中。这样,我们就得到了fold高阶函数的语义(它的另外一个名字其实就是accumulate)。如果我们将分析限制在所谓的左折叠上,那么累加器会随时存储一个中间结果,对您已经处理的子集有效。
If you have an accumulator named total
with the starting value of 0
and an array which contains the sales for the 12 months of the year, applying this definition with +
(addition) as our f
, will get you at the n-thstep the sales for the first nmonths of the year.
如果您有一个以total
的起始值命名的累加器0
和一个包含一年中 12 个月销售额的数组,应用此定义并使用+
(addition) 作为我们的f
,将使您在第n步获得第一次的销售额一年中的n个月。
Your examples look good then, as they're strictly adherent to these definitions of accumulators and counters.
那么您的示例看起来不错,因为它们严格遵守这些累加器和计数器的定义。
回答by John Dibling
"counter" and "accumulator" mean different things to different people. What they mean to your professor only your professor can say.
“计数器”和“累加器”对不同的人意味着不同的东西。它们对你的教授意味着什么,只有你的教授才能说。
That said, very often when someone refers to a counter what they mean is "count how many things there are [which meet these criteria]".
也就是说,很多时候当有人提到一个计数器时,他们的意思是“计算有多少东西[符合这些标准]”。
On the other hand, often when people say you should accumulate something, they mean "add up the total value of all these things [which meet these criteria]"
另一方面,通常当人们说你应该积累一些东西时,他们的意思是“将所有这些东西的总价值加起来[满足这些标准]”
Here's an example. Suppose you have an array of students, and those students have the ages:
这是一个例子。假设您有一组学生,这些学生的年龄如下:
{16, 17, 16, 19}
Count how many students are 17 or older? The answer is 2. Accumulate the total age of the students age 17 or older? The answer is 19+16=35.
数一数有多少学生在 17 岁或以上?答案是 2. 累计 17 岁或以上学生的总年龄?答案是 19+16=35。
回答by Steve Jessop
In my experience, programmers use "counter" to mean something that counts things one at a time, or occasionally a fixed interval at a time. The same doesn't necessarily apply to an "accumulator", although that's not to say that something that counts things one at a time isn'tan accumulator.
根据我的经验,程序员使用“计数器”来表示一次计数一个事物,或者偶尔一次计数一个固定间隔。这并不一定适用于“累加器”,尽管这并不是说一次计算一个东西的东西不是累加器。