Can I check in C(++) if an array is all 0 (or false)?

后端 未结 8 590
执念已碎
执念已碎 2020-12-21 01:34

Can I check in C(++) if an array is all 0 (or false) without iterating/looping over every single value and without allocating a new array of the same size (to use memc

8条回答
  •  独厮守ぢ
    2020-12-21 01:45

    You don't have to iterate over the entire thing, just stop looping on the first non-zero value.

    I can't think of any way to check a set of values other than inspecting them each in turn - you could play games with checking the underlying memory as something larger than bool (__int64 say) but alignment is then an issue.

    EDIT: You could keep a separate count of set bits, and check that is non-zero. You'd have to be careful about maintenance of this, so that setting a set bit did not ++ it and so on.

提交回复
热议问题