How to not iterate on 0:values in an Array

This is a discussion on How to not iterate on 0:values in an Array within the C++ Programming forums, part of the General Programming Boards category; I will give an example of what I am doing. I use a code that look like this: Code: std::vector<int> ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    383

    How to not iterate on 0:values in an Array

    I will give an example of what I am doing. I use a code that look like this:

    Code:
    std::vector<int> Integers(10);                                      //This is my declared Array
    
    for (int Calculate = 1; Calculate < 6; Calculate++)                //The for loop
    {
          if Integers[Calculate] > 0 
          
          {
              Do something
          }
    }
    Now is my question this:
    This for loop iterates on value from 1-5 with 1 in interval. So this meens 5 steps.

    The Integers Array has in this case 5 positions with either a value of 0 or a value of 1 that I have stored from before.
    As for the if-statement I am only interested of positions in the Array that are greater than 0, in this case positions that contain the number: 1.
    An live example could look like this for the 5 positions in the Array.

    0 1 1 0 0

    In the example I am only interested to iterate on position 2 and 3 because the if statement wont execute on the 0:s anyway.

    So what I am after is to "jump over" the 0 values. So instead of doing 5 steps in this case, only 2 will be made to increase speed...

    Is this possible in any way to do ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,786
    So what I am after is to "jump over" the 0 values. So instead of doing 5 steps in this case, only 2 will be made to increase speed...
    Not possible, since there is no way to know that a value is 0 without accessing it, and accessing it means iterating over it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,267
    Also note that your loop is missing indexes of your vector. It should look more like:
    Code:
    std::vector<int> Integers(10);                                      //This is my declared Array
    
    for (int Calculate = 0; Calculate < Integers.size(); Calculate++)                //The for loop
    {
          if Integers[Calculate] > 0 
          
          {
              Do something
          }
    }

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    okay I understand.. I was afraid that you had to iterate over it in order to know if there is a 0 value...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21