I have an array size 0 to 180 . I gotdistance of each element to the next element .
now what i want is to have an vector array . and on each vector array store the array index when frameDifference != 0 ..
so as a result . my vector should be like this .
say vector< < vector<int > > A;
A[0] have array1 size 0 to 20 , because until 20, frame Differnce was != 0 .
A[1] have array2 size 22 to 30 , because from 21 to 30 from Difference was != 0 and like at 21 we got frame difference == 0 then ignore it .
and start new array at next elemnt of A. and add the indexs of array element where frame difference of array was != 0 .
The issue is i am not understanding how can i do it . like push back two time s .
Could any one help me to start it . thanks
Edited :
I do't know how many arrays we would be needed when array Difference != 0 . so i moved to vector ;/
but i get error . index out of range , i get this run time error when i reach ot j = 21 in inner loop .
Why is hat so ?
Please help me to make it workable thanks .
I'm not clear on the question because I'm not sure what you mean by difference. However, you can have a vector of vectors, and you can set the size of the vector at each element as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <vector>
int main()
{
typedef std::vector<int> int_v;
typedef std::vector<int_v> int_vv;
int_vv a;
a.resize(2); // outer vector has 2 elements
a[0].resize(21); // a[0] has size 21
a[1].resize(11); // a[1] has size 11
a[0][3] = 7; // place some value in the thing
return 0;
}
//k now when j 24 ! =0 then onditon checked and at k = 3 , we shouould have j 24 a
//mext 25 .. but here again i get out of rnage run time error .
int j = 0 , k = 0;
std::vector< std::vector<int> > vv;
vv.push_back(std::vector<int>());
for(k ; k < 180; k++)
{
for(j ; j < 180; j++)
{
if( arrayDiff[j] != 0 )
{
vv[k].push_back(j); //data saved at idnex
}
else
{
break; //ignore index and not save it .
}
}
j++;
}
Note : where arrayDiff is array[i] - array[i+1] with size 0 to 180 .
coder777 has provided a great solution - try doing that first. I just wanted to add something. Hopefully it doesn't confuse the issue.
Edit: Others have provided great solutions too.
Did you read about the push_front function for vectors? It puts things at the front of the array rather than the end. This is handy if you already have things in the vector & you want to make sure the new things are at the front.
The alternative is to not specify the vector size - then push_back the new things.
This is the same topic as your previous thread - did you not find a solution from the advice given there?
How can we delete an element of outer vector array if size of inner vector array is == 0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//if size of inner array is 0. then remove the index of vv.
for(k = 0 ; k < 179; k++)
{
cout<< "k @ : " << k << endl;
for(j = 0; j < 179; ++j)
{
int innerArrayEleSize = vv[k].size();
if( innerArrayEleSize == 0 )
{
//how can we remove element of array ?
}
}
}
The first erase() should work. The problem are the subsequent erase(). The positions of the following elements are k-1.
You might want to do the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
for(k = 0 ; k < vv.size(); k++)
{
int innerArrayEleSize = vv[k].size();
if( innerArrayEleSize == 0 )
{
// erase the ith element
vv.erase (vv.begin()+k);
--k; // Be careful: for the first element k is -1 -> invalid!
}
//cout<< "Size @ : " << k << " , " << innerArrayEleSize << endl;
}