-2

This is my third question on this topic, Instead of asking a new question in the comments I thought it would be better to start a new thread.

The full code can be found here: C++ CvSeq Accessing arrays that are stored

And using the following code I can display the most recent vector that has been added to the RECT array(Note that this is placed inside of the for loop):

    RECT& lastRect = detectBox->back();
    std::cout << "Left: " << lastRect.left << std::endl;
    std::cout << "Right: " << lastRect.right << std::endl;
    std::cout << "Top: " << lastRect.top << std::endl;
    std::cout << "Bottom: " << lastRect.bottom << std::endl;

What I am now trying to do is create a loop outside of this for loop that will display all of the vectors present in detectBox. I havent been able to determine how many vectors are actually present in the array, and therefore cannot loop through the vectors.

I tried using the following:

int i = 0;
while ((*detectBox)[i].left!=NULL)
{
    std::cout << "Left: " << (*detectBox)[i].left << std::endl;
    std::cout << "Right: " << (*detectBox)[i].right << std::endl;
    std::cout << "Top: " << (*detectBox)[i].top << std::endl;
    std::cout << "Bottom: " << (*detectBox)[i].bottom << std::endl;
    i++;
}

And have also tried playing around with sizeof(*detectBox) , but only have an answer of 32 being returned...

Community
  • 1
  • 1
James Mallett
  • 827
  • 4
  • 11
  • 27
  • What is `detectBox`? Is it a `std::vector`? Then use a [range for loop](http://en.cppreference.com/w/cpp/language/range-for). – Some programmer dude Oct 02 '16 at 08:36
  • Its declaration is as follows: _std::vector detectBox;_ – James Mallett Oct 02 '16 at 08:38
  • Besides using [range for loops](http://en.cppreference.com/w/cpp/language/range-for) just about any [good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) will tell you how to iterate over a vector. The way you do it is wrong and will lead to the loop going out of bounds and give you *undefined behavior*. – Some programmer dude Oct 02 '16 at 08:43
  • The thing is, that because of the type of array it is I cant determine how many vectors there are stored in it... Once I can find out how to figure that out I will be fine with the loops. but because of the pointer and the RECT type I am struggling to get its size – James Mallett Oct 02 '16 at 08:47

1 Answers1

1

Okay, you are using the wrong terms here. The variable detectBox is a vector (or rather a pointer to a vector it seems). There are three ways to iterate over it (I'll show them a little later). It is not an array, it is not an array of vectors. It is a pointer to a vector of RECT structures.

Now as for how to iterate over the vector. It is like you iterate over any vector.

The first way is to use the C way, by using indexes:

for (unsigned i = 0; i < detectBox->size(); ++i)
{
    RECT rect = detectBox->at(i);
    std::cout << "Left: " << rect.left << std::endl;
    ...
}

The second way is the traditional C++ way using iterators:

for (std::vector<RECT>::iterator i = detectBox->begin();
     i != detectBox->end();
     ++i)
{
    std::cout << "Left: " << i->left << std::endl;
    ...
}

The last way is to use range for loops introduced in the C++11 standard:

for (RECT const& rect : *detectBox)
{
    std::cout << "Left: " << rect.left << std::endl;
    ...
}

The propblem with your attempt of the loop, with the condition (*detectBox)[i].left!=NULL is that the member variable left is not a pointer and that when you go out of bounds you are not guaranteed to have a "NULL" value (instead it will be indeterminate and will seem random).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks so much, It works perfectly. Apologies for my confusion, this is for a project that I have taken over, so I have been trying to figure out how the code is all tied together – James Mallett Oct 02 '16 at 09:01