迭代从 N 到 0(含)的所有值以获得无符号值
我有这段代码适用于常规有符号整数,我正在尝试编写一个与 size_t 一起使用的等效版本(就像现在开始和计数是整数,我需要它们是 size_t):
int count,start;
for (start = (count-2)/2; start >=0; start--)
{
someFunction( x, start, count); // x is to illustrate function has other parameters
}
我觉得这样代码对于一个非常简单的解决方案来说足够简单,但我画了一个空白。
I have this code that works fine for regular signed integers that I am trying to write an equivalent version that will work with size_t (as in that as of now start and count are ints and i need them to be size_t) :
int count,start;
for (start = (count-2)/2; start >=0; start--)
{
someFunction( x, start, count); // x is to illustrate function has other parameters
}
I feel like this code is straight forward enough for a really simple solution but I am drawing a blank.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以这样重写它:
否则,我能想到的唯一其他选择是在有符号和无符号之间进行一些非标准兼容的转换...或者用
~(size_t)0
做一些事情...以下是一些不符合标准的替代方案:
You could rewrite it like this:
Otherwise, the only other option I can think of is to do some non-standard compliant casting between signed and unsigned... or to do something with
~(size_t)0
...Here are some non-standard compliant alternatives:
编辑如果OP确实想为 cnt=1 循环一次,则需要一个三元组:
EDIT if the OP really wants to loop once for cnt=1, a ternary is necessary:
只使用减一的值怎么样?
What about just using a value off by one?
您可以修复原始代码中的结束条件。
-1/2
在 C99 中保证为 0,这使得循环体执行一次,因此如果仍然需要,您可能需要特别对待count == 1
类型未签名后的行为。这是可行的,因为我们知道
start
的初始值不可能是SIZE_MAX
,因为除以 2 后不存在size_t
的值SIZE_MAX
。对于更一般的循环,可能需要从
SIZE_MAX
开始,一直到 0(含),显然我们不能在循环体之前执行退出检查,因为我们希望循环体执行一次对于size_t
的每个值,因此没有可以退出的值。为了允许这种情况:在所有情况下,
SIZE_MAX
都可以替换为-1
,它更通用,因为它会转换为每个无符号类型的最大值,但是导致令人困惑的问题。You can fix the end condition from your original code.
-1/2
is guaranteed to be 0 in C99, which makes the loop body execute once, so you might have to treatcount == 1
specially if that's still the required behavior once the types are unsigned.This works because we know that the initial value of
start
cannot possibly beSIZE_MAX
, because there is no value ofsize_t
that when divided by 2 yieldsSIZE_MAX
.For more general loops, that might need to start at
SIZE_MAX
and go all the way down to 0 inclusive, clearly we cannot perform the exit check before the loop body, because we want the loop body to execute once for every value ofsize_t
, so there is no value on which we can exit. To allow for that case:In all cases,
SIZE_MAX
can be replaced with-1
, which is more generic in that it converts to the max value of every unsigned type, but leads to confused questions.