0

First off, I'm new to C++ and very accustomed to working in MatLAB. My code will probably want to make seasoned C++ users shoot me in the face, but it looks like this:

EDIT: I have heavily edited my code snippet. The following is a cleaned up, generalized example of what I'm trying to accomplish.

int main()
{


int t = 0;
vector<int> Pad_Ref_Vec; //initialize vector. Required size unknown


     for (int n = 0; n <= 10; n++)
     {

          if (t == 0)
          { 
               vector<int> Pad_Ref_Vec(100); //at this point in the code, i know the size i need this vector to be

               for (int i = 0; i < 100; i++)
               {
                    Pad_Ref_Vec[i] = i;         
               }

           }
           else
           {

            //do some operation using the values of Pad_Ref_Vec created in the above 
            //if statement  

            }

     t++;

     }

    return 0;
}

If I do this, the vector Pad_Ref_Vec does not read [0 1 2 3 4 5 ... ... ] after the if statement, but goes back to its previous form (after the first initialization before the for loop) which is just a vector of size 0

I'm finding it hard to believe that something so simple is turning out to be such a hassle. Thanks in advance for any constructive tips.

  • Hint: Vectors can be copied, and reinitialized from a copy. – πάντα ῥεῖ Dec 02 '16 at 15:08
  • In short, I want to use the elements of a vector which I create in an if loop (if t ==0)) in every subsequent loop (if t>0)). I'm trying to auto-correlate the intensity profile (of a line across an image) of every t>0 frame against the first t==0 frame in order to track the movement of a feature. I have a very well-developed Matlab script for this which we are using in my lab, but I am re-writting it for a real-time application using openCV. – Oliver Kress Dec 02 '16 at 15:12
  • You're chatty man. Concentrate on the relevant and important things. – πάντα ῥεῖ Dec 02 '16 at 15:19
  • [Why is “using namespace std” considered bad practice?](http://stackoverflow.com/q/1452721/995714). And why include `stdio.h` when you used `iostream`? – phuclv Dec 02 '16 at 15:58
  • @LưuVĩnhPhúc Good point. I wrote the includes back when I started this code and had even less of a clue about C++ than I do now. The only namespaces I plan to use for this are std and cv, and as far as I they don't conflict – Oliver Kress Dec 02 '16 at 16:08

1 Answers1

1

Several things here.

Most important thing, is lifetime of what you declare. If you declare a vector inside a {}, it won't be available outside of it. So if you want to use your vector outside of a if if (t == 0), you have to declare it outside of the if, else it won't exist anymore when you want to access it.

You can declare it outside of the if, and just initialize it when you want with a if. Or if you just want to add elements without really knowing the size, use push_back

Vector class has a lot of usefull function, to will help you in this situation : http://en.cppreference.com/w/cpp/container/vector (changed from cplusplus.com following comments)

David Levy
  • 430
  • 5
  • 18
  • Thanks. I've been reading into the vector class. I know I'm missing something fundamental. So if I declare it before the if (t==0) loop, then construct it inside the if (t==0) loop, can I use it after the if loop is finished? What is the difference between declaring and initializing a vector? It seems to me that declaring it automatically initialized it to zero, meaning it would just zero out during the next loop. – Oliver Kress Dec 02 '16 at 15:21
  • I'd recommend to use [cppreference](http://en.cppreference.com/w/cpp/container/vector/vector) as c++ reference. It's more reliable than cplusplus.com. – πάντα ῥεῖ Dec 02 '16 at 15:21
  • 1
    @OliverKress 1st of all `if` doesn't constitute a _loop_. – πάντα ῥεῖ Dec 02 '16 at 15:22
  • Thanks. I have mostly been using cplusplus.com. Also, sure, an if statement is a one time thing I guess. – Oliver Kress Dec 02 '16 at 15:24
  • You have to think about the lifetime of your object, if you declare it (it is initialized at declaration, but you can reinitialize it later if needed) inside the loop, at the end of the iteration, the item does not exist anymore, and a new one is created. If you need to share data between iteration, declare it outside the for loop – David Levy Dec 02 '16 at 15:29
  • @DavidLevy I tried that. I figured I could initialize the vector after `int main()` but before the main for loop. Then, I could fill it with the values I needed during the first iteration at t==0 (inside the if statement), and then operate on it during all subsequent iterations (inside the following else statement). Even after doing this, the vector is reset to its initialized form immediately after the first if t==0 statement. – Oliver Kress Dec 02 '16 at 15:43
  • Could you put this code into your question, would be easier to find the error – David Levy Dec 02 '16 at 15:45
  • @DavidLevy I edited my original post to include only the relevant code – Oliver Kress Dec 02 '16 at 16:09
  • Just tried your code, I can use Pad_Ref_Vec data inside the else – David Levy Dec 02 '16 at 16:22
  • @DavidLevy yea..tried my own code. worked. I over idealized the example code. I edited it again to most accurately reflect my code. The problem is that during initialization, I have no idea how big the vector will have to be until just before the if statement. If I try to re-initialize within the if statement for the vector to assume the proper size, then I goes back to the vector it once was once the if statement is complete. – Oliver Kress Dec 02 '16 at 16:45
  • @DavidLevy Ok. Correct usage of `.push_back` was the key here. Problem solved. Thanks! – Oliver Kress Dec 02 '16 at 16:54
  • Rookie mistake, you declare a new vector inside your if. So you modify the vector declared inside the if, not the other one. It is a very bad practise, because its really hard to know what vector you are modifying hence your error. If you don't know the size of your vector, use resize or push_back the new elements. Push_back increase the size of the vector to insert the new element – David Levy Dec 02 '16 at 16:56