By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,409 Members | 938 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,409 IT Pros & Developers. It's quick & easy.

Design question; container choice...

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
deancoo wrote:
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.

--
Regards,
Matthias
Jul 23 '05 #2

P: n/a
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).
--
Regards,
Matthias
Jul 23 '05 #3

P: n/a
deancoo wrote:
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.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #4

P: n/a
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
"Ioannis Vranos" <iv*@remove.this.grad.com> wrote in message
news:1108639926.28728@athnrd02...

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


--
Ioannis Vranos

http://www23.brinkster.com/noicys

Jul 23 '05 #5

P: n/a
deancoo wrote:
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)>.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.