List of lists

Apr 12, 2010 at 6:59pm
Hi I'm writing a program that has an object of lists and a list of that object type. I'm having difficulty figuring out how to load the data into the list of lists line 134 I use the push_back() function and that seems to be the problem. I feel like I have tried everything and it is likely that I haven't so if you could give some suggestions that would be great.

Errors:
[dtrojand@work3 ~/example]$ make
g++ -O -Wall -fPIC -O2 -Wall -fPIC -pthread -m64 -I/opt/root/include -pthread
ntupview.cxx: In function âint main()â:
ntupview.cxx:134: error: no matching function for call to âstd::list<clstr, std:â
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_li_Tp&) [with _Tp = clstr, _Alloc = std::allocator<clstr>]
make: *** [ntupview.o] Error 1

thank you for checking it out.


file ntupview.cxx
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
   1 #include<iostream>
      2 #include<fstream>
      3 #include<stdlib.h>
      4 #include <assert.h>
      5 #include <TROOT.h>
      6 #include <TApplication.h>
      7 #include <TTree.h>
      8 #include <TFile.h>
      9 #include <TBrowser.h>
     10 #include <TStyle.h>
     11 #include <list>
     12 #include <math.h>
     13 #include "cosmic_class.h"
     14 using namespace std;
     15
     16
     17 int checkmatch(list<padfire>::iterator,list<padfire>);
     18 int  shareside(list<padfire>::iterator,list<padfire>::iterator,list<padfire>);
     19 void listprint(list<padfire>);
     20
     21 int checkmatch(list<padfire>::iterator ai,list<padfire> b)
     22         {int temp=0;
     23                for(list<padfire>::iterator bi = b.begin(); bi != b.end();bi++)
     24                         {
     25                         if( ((*ai).x ==(*bi).x) && ((*ai).y ==(*bi).y))
     26                                 temp=1;
     27
     28                         }
     29         return temp;
     30         }
     31
     32 int shareside(list<padfire>::iterator ai,list<padfire>::iterator bi,list <padfire> b)
     33         { int temp=0;
     34               if(b.size()==0)
     35                 return 0;
     36                       for(bi = b.begin(); bi != b.end();bi++)
     37                               {
     38                                 if((*ai).z==(*bi).z)
     39                                   {if( ((*ai).x ==(*bi).x) || ((*ai).y ==(*bi).y))
     40                                               {temp=1;
     41                                                 return temp;
     42                                               }
     43                                   }
     44                              }
     45           return temp;
     46         }
     47
     48 void listprint(list<padfire> a)
     49         {
     50          for(list<padfire>::iterator g = a.begin(); g!=a.end();g++ )
     51                  {cout<< (*g).x <<" "<<(*g).y <<" "<< (*g).z <<   endl;}
     52         return;
     53         }
     54
     55 //###########################
     56 //###########################
     57
     58 int main( )
     59 {
     60
     61         char *filename2= "run300649.txt";
     62         // sprintf(filename2,"run%s.txt",filename);
     63         cout<<filename2<<endl;
     64         ifstream input;
     65         input.open(filename2);
     66         int garbage,count=0,flg=0;
     67         int ix,iy,iz,itime;
     68
     69
     70
     71
     72         list<padfire> tlist,cevent,intercls,ceventcls;  // total list, current event, current cluster
     73         list<int> etrack;
     74         list<clstr> clusters;
     75         list<padfire>::iterator iter = tlist.begin();
     76         list<padfire>::iterator iter2 = cevent.begin();
     77         list<clstr>::iterator iter3 = clusters.begin();
     78         list<padfire>::iterator iter4 = (*iter3).cls.begin();
     79         list<padfire>::iterator iter5 = intercls.begin();
     80         padfire templ;
     81         input >> garbage;
     82         input >> garbage;
     83         input >> garbage;
     84         input >> garbage;
     85
     86
     87         do{
     88           input >>itime;
     89           input >>ix;
     90           input >>iy;
     91           input >>iz;
     92           //cout<< itime << " " << ix <<" "<< iy <<" "<< iz << endl;
     93           templ.time=itime;
     94           templ.x=ix;
     95           templ.y=iy;
     96           templ.z=iz;
     97           tlist.push_back(templ);
     98           if(ix==-1)
     99                 {etrack.push_back(count);}
    100           count++;
    101           } while(!input.eof());
    102
    103
    104         list<int>::iterator a=etrack.begin();           //declaring iterators to loop through the lists
    105         list<padfire>::iterator cit=cevent.begin();
    106
    107
    108         for(int l=0;l<*a+1;l++){iter++;}        //finds which line the first event ends on
    109
    110         cevent.splice(cevent.end(),tlist,tlist.begin(),iter);   //take the first event from tlist and put it into cevent
    111
    112         cout<< "pre clustering test"<<endl;
    113
    114 //################################
    115 //##############CLUSTERING########
    116 //################################
    117
    118                 // clustering the layers of the current event
    119
    120
    121              do {flg=0;
    122                 intercls.push_back(*cit);
    123                 cit = cevent.erase(cit);
    124                 flg = shareside(iter5,iter2,cevent);
    125                 if (flg==1)
    126                         {while (flg==1)
    127                                 {intercls.push_back(*iter2);
    128                                 cevent.erase(iter2);
    129                                 flg=0;
    130                                 flg = shareside(iter5,iter2,cevent);
    131                                 }
    132                         }
    133                 else{
    134                         clusters.push_back(intercls); //here needs to copy all of intercls into a part of clusters
    135                         iter5 = intercls.erase(iter5);  // needs to erase all of intercls
    136
    137                         // ###### need to increment the clusters variable so its pointing at the next list########
    138
    139                     }
    140
    141                 } while(cit != cevent.end());
    142
    143   input.close();
    144   return0;
    145   }


file cosmic_class.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using namespace std;

class padfire{

public:
       int time,x,y,z;

     };

class clstr{
public:
        list<padfire> cls;

        };



Apr 12, 2010 at 7:50pm
You may want to consider hold a list of pointers to list.
e.g.
1
2
3
4
5
6
7
8
9
10
11
typedef std::list<padfire> padfire_list_type;
typedef std::list<padfire_list_type*> cluster_type;

//...
cluster_type cluster;
if (padfire_list_type* plist1 = new padfire_list_type)
{
    plist1->push_back(padfire());
    //...
    cluster.push_back(plist1);
}


Alternatively, you can stick with values, but you'll need to create an new empty list in the list of lists, then fill it in.
e.g.
1
2
3
4
5
6
7
8
9
typedef std::list<padfire> padfire_list_type;
typedef std::list<padfire_list_type> cluster_type;

//...
cluster_type cluster;
cluster.push_back(pad_list_type());

cluster::iterator p = cluster.begin();
p->push_back(padfire());
Last edited on Apr 12, 2010 at 7:56pm
Apr 15, 2010 at 7:42pm
Thank you for the response, I have fixed part of my code and am having problems printing the sub lists in the list clusters. I want to do this so that I can check to see if my algorithm is working properly.


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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
      1 #include<iostream>
      2 #include<fstream>
      3 #include<stdlib.h>
      4 #include <assert.h>
      5 #include <TROOT.h>
      6 #include <TApplication.h>
      7 #include <TTree.h>
      8 #include <TFile.h>
      9 #include <TBrowser.h>
     10 #include <TStyle.h>
     11 #include <list>
     12 #include <math.h>
     13 #include "cosmic_class.h"
     14 using namespace std;
     15
     16 list<padfire>::iterator cit;
     17
     18 int checkmatch(list<padfire>::iterator,list<padfire>);
     19 int shareside(list<padfire>::iterator,list<padfire>::iterator,list<padfire>);
     20 void listprint(list<padfire>);
     21 //void clustersprint(list<list<padfire>*>,list<padfire>::iterator);
     22
     23 int checkmatch(list<padfire>::iterator ai,list<padfire> b)
     24         {int temp=0;
     25                for(list<padfire>::iterator bi = b.begin(); bi != b.end();bi++)
     26                         {
     27                         if( ((*ai).x ==(*bi).x) && ((*ai).y ==(*bi).y))
     28                                 temp=1;
     29
     30                         }
     31         return temp;
     32         }
     33
     34 int shareside(list<padfire>::iterator ai,list<padfire>::iterator bi,list<padfire> b)
     35         { int temp=0;
     36               if(b.size()==0)
     37                 return 0;
     38                       for(bi = b.begin(); bi != b.end();bi++)
     39                               {
     40                                 if((*ai).z==(*bi).z)
     41                                  {if( ((*ai).x ==(*bi).x) || ((*ai).y ==(*bi).y))
     42                                               {temp=1;
     43                                                 return temp;
     44                                               }
     45                                   }
     46                              }
     47           return temp;
     48         }
     49
     50 void listprint(list<padfire> a)
     51         {
     52          for(list<padfire>::iterator g = a.begin(); g!=a.end();g++ )
     53                  {cout<< (*g).x <<" "<<(*g).y <<" "<< (*g).z <<   endl;}
     54         cout<<" im working"<<endl;
     55         return;
     56         }
     57
     58  //void clustersprint(list<list<padfire>*> a,list<padfire>::iterator b)
     59 //      { for(list<list<padfire>*>::iterator i = a.begin(); i!= a.end();i++)
     60 //              {listprint(i.b);}
     61 //      }
     62
     63 //###########################
     64 //###########################
     65
     66 int main( )
     67 {
     68
     69         char *filename2= "run300649.txt";
     70         // sprintf(filename2,"run%s.txt",filename);
     71         cout<<filename2<<endl;
     72         ifstream input;
     73         input.open(filename2);
     74         int garbage,count=0,flg=0;
     75         int ix,iy,iz,itime;
     76
     77
     78
     79
     80         list<padfire> tlist,cevent,intercls,ceventcls;  // total list, current event, current cluster
     81         list<int> etrack;
     82         list<list<padfire>*> clusters;
     83         //list<padfire>::iterator iter = tlist.begin();
     84         //list<padfire>::iterator iter2 = cevent.begin();
     85         //list<list<padfire>*>::iterator iter3 = clusters.begin();
     86           //?????       list<padfire>::iterator iter4 = (*iter3).cls.begin();
     87         //list<padfire>::iterator iter5 = intercls.begin();
     88         padfire templ;
     89
     90         input>>garbage;
     91         input>>garbage;
     92         input>>garbage;
     93         input>>garbage;
     94
     95
     96         do{
     97           input >>itime;
     98           input >>ix;
     99           input >>iy;
    100           input >>iz;
    101           //cout<< itime << " " << ix <<" "<< iy <<" "<< iz << endl;
    102           templ.time=itime;
    103           templ.x=ix;
    104           templ.y=iy;
    105           templ.z=iz;
    106           tlist.push_back(templ);
    107           if(ix==-1)
    108                 {etrack.push_back(count);}
    109           count++;
    110           } while(!input.eof());
    111
    112
    113         list<int>::iterator a=etrack.begin();           //declaring iterators to loop through the lists
    114         list<padfire>::iterator iter = tlist.begin();
    115
    116         for(int l=0;l<*a;l++){iter++;   //finds which line the first event ends on
    117
    118         cevent.push_back(*iter);        //take the first event from tlist and put it into cevent
    119         }
    120         cout<< "pre clustering test"<<endl;
    121
    122 //################################
    123 //##############CLUSTERING########
    124 //################################
    125         listprint(cevent);
    126                 // clustering the layers of the current event
    127
    128
    129          cit=cevent.begin();
    130
    131              do {flg=0;
    132                 list<padfire>::iterator iter2 = cevent.begin();
    133                 intercls.push_back(*iter2);
    134
    135                 iter2 = cevent.erase(iter2);
    136                 list<padfire>::iterator iter5 = intercls.begin();
    137                 flg = shareside(iter5,iter2,cevent);
    138
    139                 cout<< " first loop check "<<endl;
    140
    141                 if (flg==1)
    142                         {
    143                         cout<< " In the if " <<endl;
    144
    145                         while (flg==1)
    146                                 {intercls.push_back(*iter2);
    147                                 iter2 = cevent.erase(iter2);
    148                                 flg=0;
    149                                 iter5 = intercls.begin();
    150                                 flg = shareside(iter5,iter2,cevent);
    151                                 }
    152                         }
    153                 else{       cout<< " in the else "<< endl;
    154                         list<padfire>* clstr = new list<padfire>(intercls);
    155                         clusters.push_back(clstr); //here needs to copy all of intercls into a part of clusters
    156                         intercls.clear();       // needs to erase all of intercls
    157                     }
    158                 cit = cevent.begin();
    159                  cout<< " I made it out of the if else " <<endl;
    160                 } while(cit != cevent.end());
    161
    162
    163
    164
    165          //for(list<list<padfire>*>::iterator g = clusters.begin(); g!= clusters.end();g++)
    166                //   {listprint(g);}
    167
    168         list<list<padfire>*>::iterator g = clusters.begin();
    169
    170
    171         cout << "Last check" << endl;
    172
    173         input.close();
    174
    175    return 0;
    176 }
    177
    178
    179
    180
    181


the file cosmic_class.h is still the same.

I attempted to make a function to print the clusters variable which uses a function I made before "listprint" and I have no idea how to access the sublists to print them. You can see what I tried and there were a few other things that didn't work. If anyone could give me some ideas I would appreciate it. Thank you.
Apr 15, 2010 at 8:09pm
nvm figured it out
Apr 15, 2010 at 8:38pm
You're passing lists by value to the helper functions.
Topic archived. No new replies allowed.