vector<vector<Player>> iterator

I am writing a tennis tournament simulator. I have two main classes to help me achieve this, a 'Player' class and a 'Tournament' class. Unsurprisingly the 'player' class contains details about each individual player, and the 'Tournament' class details of the tournament to be played (I am trying to simulate the entire ATP Tour).

I store each instance of the 'Tournament' class in a vector as below. For now I am doing just one tournament.

1
2
vector<unique_ptr<Tournament>> GrandSlams;
GrandSlams.emplace_back(new Tournament("Wimbledon","Grass",128,7,32,4,{10,45,90,180,360,720,1200,2000}));


I pass, and correct me if the terminology I use is wrong here as this is quite new to me and I am hacking it together by trial and error more than by understanding fully, the address of the pointer to each tournament to a 'PlayTournament' function as below.

1
2
3
for(auto &T: GrandSlams) {
   PlayTournament(Players,T);
}


In the 'PlayTournament' function I am first wanting to resize the vector of vectors which will represent the tournament Draw. i.e. for a 128 person tournament I have this as vectors of Player objects of size 128,64,32,16,8,4, and 2.

I have so far only been able to do this looping over in the classic way as you can see below, but I want this to be 'good' code and use iterators or auto. You can see I have tried a number of different ways but none of them work. When I compile I get the error "no match for 'operator[]' (operand types are 'std::vector<std::vector<Player> >' and 'std::vector<std::vector<Player> >::iterator {aka __gnu_cxx::__normal_iterator<std::vector<Player>*, std::vector<std::vector<Player> > >}')|"

1
2
3
4
5
6
7
8
9
10
11
void PlayTournament(vector<Player> &Players,unique_ptr<Tournament> &T){
    //Get size of initial draw and resize draws for each round.
    int DrawSize = T->GetDrawSize();
    int NoOfRounds = T->GetNoOfRounds();
//    vector<vector<Player>>::iterator itr1;
//    for(auto itr1=T->Draw.begin();itr1!=T->Draw.end();++itr1){
//    for(itr1=T->Draw.begin();itr1!=T->Draw.end();++itr1){
    for(int itr1=0;itr1<NoOfRounds;itr1++){
        T->Draw[itr1].resize(DrawSize);
        DrawSize/=2;
    }


There are a number of places I wanted to use iterators so I would appreciate any help you might be able to give. If you need more information or code let me know.
Since you haven't shown us the class declaration for Tournament, I can only guess at the members.

Line 9 looks suspicious. You're using itr1 as a subscript to Draw, but based on line 6 and 7 Draw appears to be an unsubscripted variable.

When dealing with vectors of vectors, I usually find it useful to typedef the outer vector. e.g.
 
typedef vector<Player> PLAYERS;

This makes the code easier to read and I often find that I subsequently want to enhance the typedef'ed vector into a full blown class and it becomes simple to do so.

Thanks for the quick reply. If it helps, the class declaration for Tournament is below. The code as it is in my previous post works, so line 9 is resizing the vector stored in each cell(?) of the outer vector. Lines 5+7 together, and line 6 were my other attempts to do the same thing. Thank you for the typedef tip, I will try and use that.

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
class Tournament{
protected:
    string Name;
    string Surface;
    int DrawSize;
    int NoOfRounds;
    int NoOfSeeds;
    int Wildcards;
    vector<int> Points;

public:
    // Data members
    vector<vector<Player>> Draw;
    // Member functions
    Tournament(){};
    Tournament(string n,string s,int ds,int nor,int nos,int w,vector<int> p):
    Name(n),Surface(s),DrawSize(ds),NoOfRounds(nor),NoOfSeeds(nos),Wildcards(w),
    Points(p){Draw.resize(NoOfRounds);}
    string GetName(){return(Name);}
    string GetSurface(){return(Surface);}
    const int GetDrawSize(){return(DrawSize);}
    const int GetNoOfRounds(){return(NoOfRounds);}
    const int GetNoOfSeeds(){return(NoOfSeeds);}
    const int GetWildcards(){return(Wildcards);}
    const int GetPoints(int i){return(Points.at(i));}
};
Topic archived. No new replies allowed.