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

后端 未结 8 591
执念已碎
执念已碎 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:43

    You can use the following condition:

    (myvector.end() == std::find(myvector.begin(), myvector.end(), true))
    

    Obviously, internally, this loops over all values.

    The alternative (which really should avoid looping) is to override all write-access functions, and keep track of whether true has ever been written to your vector.

    UPDATE

    Lie Ryan's comments below describe a more robust method of doing this, based on the same principle.

提交回复
热议问题