Design question; container choice...

D

deancoo

Being fairly new to STL, I'm looking for a little advice on which container
to use. Let me set up the scenario for you to consider. Suppose you're
trying to model a hotel. The hotel has a fixed number of hotel rooms (10
say), each of which may or may not contain a guest. Guests come and go.
You would at some point need to visit each guest in sequence (to clean
rooms), and you will need to visit them independently (for say a package
delivery).

Because of the fixed number of rooms in this example, I was originally
thinking about using an associative container like map, where you could
associate an int (room number) with an object (guest). As guests come or
go, you assign or delete objects from the map as needed. However, I'm not
so sure how well a map iterates through each of its elements compared to a
sequence container. Anyway, any advice offered would be appreciated.

d
 
M

Matthias

deancoo said:
Guests come and go.
You would at some point need to visit each guest in sequence (to clean
rooms), and you will need to visit them independently (for say a package
delivery).

Hm, on the one hand, if guests are associated with some sort of key, a
map sounds like a reasonable choice. On the other hand, if you want to
have random access iterators, map isn't so good after all (only
contiguous memory containers support that).
It depends on what you mean with "visit them independently".
Because of the fixed number of rooms in this example, I was originally
thinking about using an associative container like map, where you could
associate an int (room number) with an object (guest). As guests come or
go, you assign or delete objects from the map as needed. However, I'm not
so sure how well a map iterates through each of its elements compared to a
sequence container. Anyway, any advice offered would be appreciated.

Associative containers (sets and maps) are (probably almost always)
built around balanced trees, so deleting objects from the middle will
only result in a rebalancing operation, whereas deleting from a vector
or deque could end up in a lot of copying, since every object above the
one inserted or deleted would have to be moved up/down.

As far as iterating over elements is concerned, I don't think there are
big differences, except the one mentioned above: Only contiguous memory
containers (vector, deque, string) allow iterator arithmetic.

So, if you don't need to e.g. sort your containers, using associative
containers sounds like a reasonable approach.
 
M

Matthias

Maybe one more thing:
maps do /not/ access elements through a key in constant time, but in
logarithmic time. If you want constant time access, you may want to look
for a hashed container (which are not part of the standard).
However, if performance isn't an extreme must have for your application,
maps will do just fine (logarithmic complexity is still very good,
especially for large amounts of data).
 
I

Ioannis Vranos

deancoo said:
Being fairly new to STL, I'm looking for a little advice on which container
to use. Let me set up the scenario for you to consider. Suppose you're
trying to model a hotel. The hotel has a fixed number of hotel rooms (10
say), each of which may or may not contain a guest. Guests come and go.
You would at some point need to visit each guest in sequence (to clean
rooms), and you will need to visit them independently (for say a package
delivery).

Because of the fixed number of rooms in this example, I was originally
thinking about using an associative container like map, where you could
associate an int (room number) with an object (guest). As guests come or
go, you assign or delete objects from the map as needed. However, I'm not
so sure how well a map iterates through each of its elements compared to a
sequence container. Anyway, any advice offered would be appreciated.


Will you associate rooms with names? Please provide all data
associations and their use.
 
D

deancoo

Yes. The 'room' needs to be linked in some way to the 'guest'. The room
may also be an object of its own. Of course, both room and guest would have
an array of attributes all of which are indirectly associated to each other
through the parent association.

d
 
I

Ioannis Vranos

deancoo said:
Yes. The 'room' needs to be linked in some way to the 'guest'. The room
may also be an object of its own. Of course, both room and guest would have
an array of attributes all of which are indirectly associated to each other
through the parent association.


Well you can make a class rooms with 10 rooms (with 10 vector<unsigned>
or vector<string>).


A class guests with a multimap<string (the name of a guest), unsigned
(the index of room in the vector above)>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top