-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.

4

2 回答 2

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

于 2013-11-09T09:39:08.880 回答
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.

于 2013-11-09T09:40:09.320 回答