8

How could I check if every single element in a vector is zero without looping through them?

Current I have (In semi-MEW form): What this attempts to do is check if all three values of the final vector (its three dimensional... year.) is all zeros, (At the origin), or if it equals any previous vector on all three values.

        siteVisited = false; counter = 0;  
        while (counter < (walkHist.back().size()-1))
        {
            tdof = 1;
            while (tdof <= dimensions)
            {
                if (walkHist.back().back().at(tdof-1) == 0)
                {
                    siteVisited = true;
                }
                else
                {
                    siteVisited = false;
                    break;
                }
                tdof++;
            }
            if (siteVisited)
            {
                goto visited;
            }

            tdof = 1;
            while (tdof <= dimensions)
            {
                if (walkHist.back().back().at(tdof-1) == walkHist.back().at(counter).at(tdof-1))
                {
                    siteVisited = true;
                }
                else
                {
                    siteVisited = false;
                    break;
                }
                tdof++;
            }
            if (siteVisited)
            {
                                visited:
                ...
            }
            counter++;
        }
NictraSavios
  • 502
  • 2
  • 9
  • 29
  • Possible duplicate of [Can I check in C(++) if an array is all 0 (or false)?](https://stackoverflow.com/questions/4044590/can-i-check-in-c-if-an-array-is-all-0-or-false) – phuclv Mar 07 '19 at 08:03

3 Answers3

42

It depends on what you mean by looping, but this would work:

bool zeros = std::all_of(v.begin(), v.end(), [](int i) { return i==0; });
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 2
    The `all_of` algorithm has the added benefit that it may exit early if one of the elements isn't 0 and save some unnecessary checking. – Steve Nov 21 '13 at 21:44
  • @Steve Actually, the `count` example was pretty poor for the reasons you mention. I removed it. – juanchopanza Nov 21 '13 at 21:46
2

The reason for the check is important to know. If a zeroed array is important, and is checked regularly, it would be worth sub-classing vector to set a flag whenever a value is added or changed. This would add overhead to all adds, removes and modified, but would make the "is everything zero" test quick.

If your arrays are very often zeroed, it might then be worth writing your own sparse array class (probably based on map). When a non-zero item is added, or an element is changed to non-zero it is entered into the map. When changed to 0 it is removed. Now you know if all the elements are zero because the map will be empty. This will also have the benefit of using much less memory.

However, if the check for a zeroed array is comparatively rare, then any loop which breaks on non-zero will work well. It does, however, mean that the slowest check will be on all zero, which is worth keeping in mind.

Simon Parker
  • 1,656
  • 15
  • 37
  • Its a n-dimensional self-aware random walk. I have to check if it has wandered back to the origin. – NictraSavios Nov 22 '13 at 00:10
  • So the question is how often do you need to know this, and how likely is it. If it is unlikely, then it is probable that all n-dimension will be non-zero, so a loop with break on non-zero will normally only test one element. If it is very common, and checked regularly, then some form of simple test makes more sense. – Simon Parker Nov 22 '13 at 01:53
  • It would need to be checked with every single step. (Typically I run 10^3 walks with 10^9 steps), so a test makes the most sense. – NictraSavios Nov 30 '13 at 23:36
2

You can also check if it equals a zero vector:

if(myVector == copyVector(myVector.size(), 0)){//this is a zero vector, do stuff}

Don Larynx
  • 685
  • 5
  • 15