A football sim - some pointers on internal database design

Hello all. First up, I've been using this site alongside coding books and tutorials to learn C++. I'm enjoying it, but am still at the early stages of learning the language. Be gentle. ;-)

I'm trying to write a very basic football (soccer) simulation to teach me some of the fundamentals of using data, pointers, vectors and so on. It's all console at the moment, with the intention of looking at Win32/DirectX GUIs once I have learnt the basics. I'd like some advice as to whether I'm setting myself up for a massive fall.

I have loaded the team data from a CSV file and created a vector of objects (called Clubs). I have done the same for all the countries in the world (called Nations). This works fine in terms of loading it into the memory. I have also been able to create Competition objects which can load into their own internal ClubsList vector the indexes of the clubs whose "Clubs.competition" variable has the ID number for that competition.

What this means is that the Competitions.ClubsList vector holds the index of the specific club in the Clubs vector. So, when pulling this from a function I pass the address of the Clubs vector and then call Clubs[x] to find that club's information.

This works (for now!), but I wonder if it will fall apart once the vectors start changing in size and being moved around the memory?

I am now trying to generate schedules (using the Berger system) which works perfectly well (mathematically). I will be creating new Match objects, and then I will pass the index of that match within a Matches vector to both the club (so the club can know who they are playing) and to the competition (so it knows which results affect its standings).

The question is - is using indexes to reference these objects a good idea or not? What potential problems am I creating for myself? And is there a standard way of approaching this problem which I've managed to miss?

By way of an example, he is the code which checks the clubs vector to see which clubs belong to the specific competition:
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
void league::FindMembers(std::vector<club>& clubsVector)
{
	// Loops through the club database and checks to see which
	// belong to this league competition
	
	unsigned int sizeOfClubVector;
	sizeOfClubVector = clubsVector.size();
	int ClubsInLeague = 0;

	for (unsigned int i = 0; i < sizeOfClubVector; i++) {
		if (clubsVector[i].GetClubComp() == 1) { // uses "1" at the moment, because I only have one competition, but this will become a function parameter 
			MemberClubs.push_back(i);
			ClubsInLeague++;
		}
	}
	NumberOfTeams = ClubsInLeague;

	club ClubToPrint;
	int index;
	for (int i = 0; i < ClubsInLeague; i++)
	{
		index = MemberClubs[i];
		ClubToPrint = clubsVector[index];
		std::cout << ClubToPrint.GetClubName() << "\n";
	}
	

	return;
}
Last edited on
is using indexes to reference these objects a good idea or not?

That depends on whether you're going to be deleting entries out of your vectors.
Once you delete an item in a vector, all the subsequent indexes are invalid.

Increasing the size of a vector or the vector moving in memory won't invalidate your indexes although it will invalidate iterators.

If you're going to be deleting items, I'd suggest using std::map or std::unordered_map. You can create your map using an int as the index to the map. Deleting an entry from a map only invalidates that index.
I will be deleting entries from some of the vectors, yes. Or at least, as the program gets more complex this is likely to happen (so that matches can be unloaded from the memory once their results are no longer relevant to the current season and can be archived).

Having done a quick Google search, it looks like maps might be a good thing to look into. Thanks for the reply.
Topic archived. No new replies allowed.