-1

Ok, im setting up a really basic example agenda which has the following characteristics and behaves like this:

It will be a group of astronauts (class astronaut), that can travel from the earth to the moon and to mars, etc. Then i would like to write down the different periods(class) in which each astronaut is in each celestial body. So the agenda would have a separate text file to store the data. The following is the period and astronaut classes and what could be an example of someone using the agenda:

class astronaut
{
    int id;
    date bday;
    vector<period> V; or list<period> L;
    bool he_dead;
}

class period
{
    int CelestialBody; //0 for earth, 1 for moon, 2 for mars, etc.
    int Establishment;
    int Duty;
    date EndDate;
}

Example of someone using it:

open program
add_astronaut(01/01/2013)
close program

open program
select astronaut from list
end_period(Earth, 01/07/2013)
close program

open program
select astronaut from list
end_period(Mars, 01/01/2014)
close program

open program
select astronaut from list
end_period(Earth, 01/07/2014)
he_dead()
close program

OK, wrapping up, the idea is that you never delete any astronaut nor any data from the text file, its a register, all the deletion that happens is the program clearing its memory before closing, and after saving to the .txt. Also, there will be times in which some other fields will be left blank, for future completion.

The question is then, whats better, a list of astronauts where every astronaut has a vector of periods, or a vector of astronauts where every astronaut has a list of periods?

I'm new to the STL and some background justification would be appreciated in respect to memory management concerns.

2 Answers2

3

Given my (albeit limited) understanding of what you are trying to do, vector for both astronauts and periods should be used. They are the "default" STL container. Lists are useful primarily if you are going to do ALOT of insertions and deletions in the middle of the data.

Here's a good comparison of the two on SO: vector vs. list in STL

Community
  • 1
  • 1
It'sPete
  • 5,083
  • 8
  • 39
  • 72
1

I'd use vectors for both.

Use vectors by default: they are faster as long as you're inserting / deleting in the end of the container, and they are allocated to continuous blocks of memory, so they use less memory when small.

If one of your containers is going to be huge, you could also consider using a deque, which will allow the program to break the memory into several smaller blocks.

Shep
  • 7,990
  • 8
  • 49
  • 71
  • Thats what im worried about, i load the list with 3 astronauts, i choose the second and add a period, that would break the vector in half, reallocate, etc. But i guess is not a problem unless you go beyond ten millions astronauts or so... – 1.auxl2.1laux.2 Nov 09 '13 at 09:45
  • A vector points to memory on the heap, so the size of the object itself is constant, i.e. adding / removing stuff from it doesn't change the size of the class that contains it. – Shep Nov 09 '13 at 09:48
  • @1.auxl2.1laux.2, I think your missing an important concept. When you create a astronaut, you only have a pointer to a vector of periods to some other memory chunk that contains the periods, it's not one continuous memory block. – It'sPete Nov 09 '13 at 09:48
  • Wait, how come "vector V;" is a pointer? I thought that when you had a vector inside a class it was allocated contiguously not only inside but also with the other members of the class. Is that incorrect then? – 1.auxl2.1laux.2 Nov 09 '13 at 09:54
  • Yup, incorrect. Internally the vector points somewhere else. It has to, otherwise the class would have a non-constant size, which would make putting it in a vector impossible. – Shep Nov 09 '13 at 10:03