|
Prev: A TC++PL protected class question
Next: Deleting items from an std::list , is this code correct?
From: yogesh on 23 Apr 2008 16:04 Hi, I need to know how to solve this problem of the code , kindly help me if any one know this given 2 classes A and B defined as follows struct A { int a; } struct B { int b; char c; } the requirement is to efficiently write 2 map classes 1. class AMap It should have member functions similar to below add (const std::string name, A*) A* get (const std::string name) remove(const std::string name) 2. class BMap It should have member functions similar to below add (const std::string name, B*) B* get (const std::string name) remove(const std::string name) Note that in the above add/get/remove functions, I have deliberately not provided the correct signatures as that is what you need to create. Add a main routine that exercises all the above functions no extra libs like glib should be used; STL may be used can any one guess what this -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Nicola Musatti on 24 Apr 2008 06:18 On Apr 24, 9:04 am, yogesh <yogeshkum...(a)gmail.com> wrote: > Hi, > > I need to know how to solve this problem of the code , [...] > can any one guess what this I'll venture a guess: this is homework! If I'm correct you should try and solve the problem by yourself and come back for advice on specific, concrete issues, rather than look for people to do it for you. If I'm mistaken, well, a good way to prove me wrong is to follow my advice above ;-) Cheers, Nicola Musatti -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Alf P. Steinbach on 24 Apr 2008 06:43 * yogesh: > > I need to know how to solve this problem of the code , I'm not sure how this one slipped past moderation, but the moderators (praise be upon them![1] :-)) are only human. This is clearly homework and as such, the article should have been rejected. Note that if you don't do your homework yourself, but just ask others to do it, you will only be postponing the point where you're unable to proceed with the course (or some other course). And on the way you will then have wasted other's time and money, namely those helping you, and your own or your family's money, because instead of paying tuition and not earning wages and then failing the course or some later course, you could have been working /earning/ money. Also, please don't post the same article independently to many newsgroups, because that is an additional waste of time for those wishing to help you, and for readers. > kindly help me if any one know this > given 2 classes A and B defined as follows > struct A { int a; } > struct B { int b; char c; } > the requirement is to efficiently write 2 map classes > 1. class AMap > It should have member functions similar to below > add (const std::string name, A*) > A* get (const std::string name) > remove(const std::string name) > 2. class BMap > It should have member functions similar to below > add (const std::string name, B*) > B* get (const std::string name) > remove(const std::string name) > Note that in the above add/get/remove functions, I have > deliberately not provided the correct signatures as that is what you > need to create. The above paragraph is a message from your teacher to you. He or she is hinting that the signatures are not perfect. Consider references instead of pointers. > Add a main routine that exercises all the above functions > no extra libs like glib should be used; STL may be used If you can use "STL", presumably meaning the C++ standard library, then one way to cheat would be to use std::map. However, I'm sure your teacher meant that you could use the standard C++ library in your test program, and that you should not use std::map in the implementation of the two classes AMap and BMap. Essentially your teacher is asking you to create two classes AMap and BMap akin to std::map, except not as a template class that can handle any kinds of keys and values, but just handling strings as keys, and A and B as values. The requirement of "efficiently" probably means that a linear search for a key is not sufficient except as an optimization for small number of keys. The general search must use e.g. a binary search or a hash table lookup. Presumably the intent is to then let you realize that the two classes AMap and BMap are (or could be) identical except for the value type that a key retrieves, and then to introduce you to templates, where instead of two distinct classes AMap and BMap you can just write one general template class, like std::map. Cheers, & hth., - Alf Notes: [1] We do receive praise for our work, thank you all. Also many many thanks to those who report problems. Without knowing about problems we can't fix them. -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: red floyd on 24 Apr 2008 06:43 yogesh wrote: > ["do my homework"] redacted. Please find your answer in the FAQs at http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2 -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Stuart Golodetz on 24 Apr 2008 06:43 yogesh wrote: > Hi, > > I need to know how to solve this problem of the code , > > kindly help me if any one know this > given 2 classes A and B defined as follows > struct A { int a; } > struct B { int b; char c; } > the requirement is to efficiently write 2 map classes > 1. class AMap > It should have member functions similar to below > add (const std::string name, A*) > A* get (const std::string name) > remove(const std::string name) > 2. class BMap > It should have member functions similar to below > add (const std::string name, B*) > B* get (const std::string name) > remove(const std::string name) > Note that in the above add/get/remove functions, I have > deliberately not provided the correct signatures as that is what you > need to create. > Add a main routine that exercises all the above functions > no extra libs like glib should be used; STL may be used > > > can any one guess what this Yes, he wants you to write two classes which map strings to A* and B* respectively. I don't think the way he's specified it is especially precise, and I'm not sure A* and B* are especially good choices for a value type (who owns the memory pointed to? who should ultimately delete/free it?), but those are things you can point out in your answer. It's also worth asking what he means by "efficiently write". Should the writing be efficient, or the code? There's not always a trade-off involved, but there can be. Regards, Stu -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Pages: 1 Prev: A TC++PL protected class question Next: Deleting items from an std::list , is this code correct? |