Filling Nested Vectors

I have a nested vector named fifo_vec and a vector that is filled randomly with 0's or 1's and this vector is called with the function getChannel. both vectors have the same length of 10. What i want to do when i run the function is assign the first value of getChannel to the first component of the first vector in fifo_vec, the second value of getChannel to the first component of the second vector in fifo_vec and so on.
here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void CReadout::AssignRnd(int nchannel,double hitprob){ 
  srand( time(NULL) ); 
  double random;
  
     for( int i=0; i<nchannel; i++) { 
	  random = rand() % 10000;
	  double value = random/10000;  
	if( value < hitprob ) channel.push_back( 1 );
	else channel.push_back( 0 );
   
	   }
   for (int j=0;j<nchannel;j++)
   	   cout<< "channel " << j << "= " << channel[j] <<endl;  
}

int getChannel(int i){return channel[i];}

 void CReadout::loadFifo_vec(int i){
	
	
		for (int t=0;t<channel.size();t++) {
    		
    		if ( getChannel(t)==1){
        	 	fifo_vec[t][i]=1;
                          
            }
            else {
                fifo_vec[t][i]=0;
                         
            }
                
}
    if (i==10) i=0;
    else i++; 
    for(int y=0;y<channel.size();y++)
            cout<<"fifo_vec "<<y<<" = "<<fifo_vec[y][0]<<endl;
}



note that: vector <vector <int> > fifo_vec;
vector<int> channel;
are declared as private in the class which both these functions are defined in.
Last edited on
What error(s) are you getting? Did you ensure that fifo_vec has at least channel.size() vectors, each of length channel.size()?

Also, this does not affect i outside the method call (it's being passed by value):

1
2
if (i==10) i=0;
else i++;

Whatever you need to do with i, you should do outside this method.
Thanks for the response. That's a good idea about i. There are no errors when compiling but when i go to run my code its gets to the loadFIFO function it crashes. As far as the size of fifo_vec i delcared it as empty and the size of channel.size() is 10. How would i ensure that fifo_vec has at least channel.size() vectors, each of length channel.size()? do you think that would fix my problem.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
for (int t=0;t<channel.size();t++)
{
     if ( getChannel(t)==1)
     {
          fifo_vec[t][i]=1;
     }
     else
     {
          fifo_vec[t][i]=0;
     }             
}


This code would work based on the assumption that the size of the vector fifo_vec is at least channel.size(); and that each of the vectors fifo_vec[t] where 0 <= t < channel.size(), have at least size i. If fifo_vec is empty, then this would cause a crash.

There is probably a better way to do this, but once you know the size of channel, you should call fifo_vec.resize( channel.size() );. If you're calling loadFifo_vec in a loop with increasing i, you could call fifo_vec[t].push_back( 1 /* or 0 */);. This places the 1 or 0 as the last element of fifo_vec[t] and automatically increases its size.

If you want fifo_vec to have x int vectors, each of length y, all initialized to zero, then call this code:

fifo_vec.resize( x, vector<int>(y, 0) );

You can then index this vector as you do in your current code.
Last edited on
thank you so much for your help
Topic archived. No new replies allowed.