473,320 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Design question; container choice...

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
5 1264
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nick Moore | last post by:
This may be the wrong newsgroup for this question. I'm trying to develop a container control inherited from a panel that I can 'Roll Up' and 'Roll down' on a button click. The component is to be...
10
by: Jason | last post by:
Hi, I have a few million data items being received by my program and I wish to store them in a list, with each item being inserted in any position in the list. Any performance tips so that my...
3
by: fernandez.dan | last post by:
I'm still learning how to use Object Oriented concepts. I'm have a basic question dealing with design. I have two classes that deal with I/O pertaining to network and usb that inherit from an...
17
by: Divick | last post by:
Hi, I am designing an API and the problem that I have is more of a design issue. In my API say I have a class A and B, as shown below class A{ public: void doSomethingWithB( B * b) { //do...
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
4
by: alacrite | last post by:
I have a class that I want to turn its contents into csv file. I want to be able to set the value of the delimiter, the name of the file it gets saved to, the path of that file, and maybe a few...
48
by: Tony | last post by:
How much bloat does the STL produce? Is it a good design wrt code bloat? Do implementations vary much? Tony
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
15
by: Tim H | last post by:
I have a design opinion. I have 3 models in mind, and I don't particularly like them, so maybe someone can suggest or help justify. I have a set of essentially unrelated classes (about 6). ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
1
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.