vector inside vector

Hy guys,

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 ;/
Last edited on
ok i did like this ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::vector< std::vector<int> > vv;
	vv.push_back(std::vector<int>());
	for(k = 0; k < 180; k++)
	{
		for(j = 0; j < 180; j++)
		{
			if(  frmDifff[j] != 0 )
			{
				vv[k].push_back(j);
			}
			else
			{
				break;
			}
		}
	}


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 .
Last edited on
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;
}
Last edited on
vector size is not know.

but i did like this .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//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 .
Last edited on
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 ?
You have 180 elements of the list and on line 2 you added just one. So put line 2 -> 4:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::vector< std::vector<int> > vv;
	vv.push_back(std::vector<int>());
	for(k = 0; k < 180; k++)
	{
		vv.push_back(std::vector<int>());
		for(j = 0; j < 180; j++)
		{
			if(  frmDifff[j] != 0 )
			{
				vv[k].push_back(j);
			}
			else
			{
				break;
			}
		}
	}
or do what kbw suggested and resize the vector on line 2
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?
Last edited on
I wonder if this is the long way around for the question - provide a unique list of numbers in a vector?

If so check out the unique algorithm.
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  ? 
			}
			
		}
	}
Last edited on
Hy guys ,

I tied to remove a vector element like this
1
2
3
4
5
6
7
8
9
10
11
12
13
//if size of inner array is 0. then remove the index of vv. 
	for(k = 0 ; k < 179; k++)
	{
		int innerArrayEleSize = vv[k].size();
			if( innerArrayEleSize == 0 )
			{
				 // erase the ith element
				 vv.erase (vv.begin()+k);

			}
			//cout<< "Size @ : " << k << " , " << innerArrayEleSize  << endl;
		
	}


but its not removing the element where size is 0 .
Am i doing some thing wrong ?
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;
		
	}
Last edited on
Topic archived. No new replies allowed.