# Exercise solutions from "Accelerated C++" (Koenig/Moo)

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-26-2012
tap3ah
Exercise solutions from "Accelerated C++" (Koenig/Moo)
Post your own solutions, or comment those posted by others.
• 03-26-2012
laserlight
If you want a critique, post your own solutions ;)
• 03-26-2012
tap3ah
[11-6]
Add an operation to remove an element from a Vec and another to empty the entire Vec. These should behave analogously to the erase and clear operations on vectors.

Code:

```// from the code from the author's web site void clear() { uncreate(); } // inline```
Code:

```template <class T> typename Vec<T>::iterator Vec<T>::erase(iterator b, iterator e){   if (b != avail){     // shift elements left for e - b starting with e     for (iterator it(e), jt(b); it != avail; *jt++ = *it++);     iterator newavail(avail - e + b); // mark new off-the-end pointer     do // destroy elements from old to new off-the-end pointer       alloc.destroy(--avail);     while (avail != newavail);     shrink(); // not required   }   return b; } template <class T> typename Vec<T>::iterator Vec<T>::erase(iterator it){   return erase(it, it + 1); } // deallocates the memory so the half of the allocated memory is used template <class T> void Vec<T>::shrink(){   iterator newlimit(limit);   while (avail - data < newlimit - avail)     --newlimit;   if (limit != newlimit){     alloc.deallocate(newlimit, limit - newlimit);     limit = newlimit;   } }```
• 03-26-2012
tap3ah
[5-1]
• 03-31-2012
tap3ah
[12-1]
Reimplement the Str class, but choose an implementation strategy that requires that the class manage the storage itself. For example, you might store an array of char and a length. Consider what implications this change in design has for copy control. Also consider the cost of using Vec, (e.g., in storage overhead).
Code:

```#include <iostream> #include <cstring> #include <cctype> using namespace std; class Str{ friend         istream& operator>>(istream&, Str&); public:         Str& operator+=(const Str& s){                 copy(s.data, s.data + s.length, data + length);                 return *this;         }         typedef size_t size_type;         Str():length(0) {}         Str(size_type n, char c) {                 for (length = 0; length != n; data[length++] = c);         }         Str(const char *s) {                 copy(s, s + strlen(s), data + length);         }         char& operator[](size_type i){                 return data[i];         }         const char& operator[](size_type i) const {                 return data[i];         }         size_type size() const {                 return length;         } private:         char data[];         size_type length; }; istream& operator>>(istream& is, Str& s){         s.length = 0;         char c;         while (is.get(c) && isspace(c));         if (is){                 do                         s.data[s.length++] = c;                 while (is.get(c) && !isspace(c));                 if (is)                         is.unget();         }         return is; } ostream& operator<<(ostream& os, const Str& s){         for (Str::size_type i(0); i != s.size(); ++i)                 os << s[i];         return os; }```
• 03-31-2012
phantomotap
O_o

Getting help with something you don't understand: awesome.
Bragging to a group of mostly professionals that you understand something you don't: foolish.
Noting the authors of the exercises can solve them: pointless.

Improving an implementation of one exercise is one thing, but shotgunning the forum with a bunch of different exercises you aren't doing properly? I don't see how that would ever be useful. How are people going to focus on anything important? How are you going to keep track of every response?

The point of my post here isn't to make you feel bad.

I'm trying to draw your attention to one simple fact: you aren't advanced enough to be doing the exercises you are doing. Both solutions you've written have problems with the second being horribly broken.

You need to go back to the beginning of the book. You need to start over and do every exercise again. Then, and then only, you should post with help for the exercises you aren't getting written properly.

Soma
• 03-31-2012
tap3ah
Quote:

Originally Posted by phantomotap
The point of my post here isn't to make you feel bad.

If that's really true you would spend your time pointing out what I did wrong, instead of writing your hateful post.

The whole reason I started this thread is that I badly needed help with some previous exercises, but I could find it only for few of them.

The idea is to let people post what they think might be solutions, and the other, more knowledgeable people, correct them, comment and advise. Pretty much like every other forum.
• 03-31-2012
phantomotap
O_o

That really is true, and yet I'm still not going to walk you though the problems you have implementing the solution to an exercise from chapter 11 or 12 because you clearly are not ready for those exercises. If you want my help, go back to the earlier chapters and actually do the exercises. If you have trouble with them, start a thread relevant to that issue and I'll be happy to help.

I suppose you may convince someone else to help you with those, but I'm not interested for a very simple reason. If you aren't willing to admit you've outpaced your knowledge every second I spend helping you is wasted because you'll just keep doing it over and over again until you are so far over your head you'll be completely useless.

How do I know you've outpaced yourself? You've written code (Post: #5)that a highly conforming compiler will not even compile because it violates (the zero-sized array).

If you really want to understand the material of the later chapters you'll have to take your time. You can't magic your way to understanding more complicated material just because someone is babysitting you; you'll still have to put in time and effort.

Soma
• 04-01-2012
tap3ah
Zero-sized array was a mistake. I left it to solve it later, but forgot about it because the code DID compile (gcc 4.6.2).

I'm sure you see logic in your reasoning, but the truth is, you are just an elitist jerk who enjoys putting novice coders down.

Edit: Anyway, I just realize I can't edit old posts so this thread might be bad idea after all.
• 04-01-2012
tap3ah
[12-1] corrected
Code:

```class Str{ friend         istream& operator>>(istream&, Str&); public:         typedef size_t size_type;         Str& operator+=(const Str& s){                 char *old_data(data);                 data = alloc.allocate(length + s.length);                 copy(old_data, old_data + length, data);                 alloc.deallocate(old_data, length);                 copy(s.data, s.data + s.length, data + length);                 length += s.length;                 return *this;         }         Str():length(0) {}         Str(size_type n, char c) {                 data = alloc.allocate(n);                 for (length = 0; length != n; data[length++] = c);         }         Str(const char *s) {                 size_type n(strlen(s));                 data = alloc.allocate(n);                 copy(s, s + n, data);         }         ~Str(){                 alloc.deallocate(data, length);         }         char& operator[](size_type i){                 return data[i];         }         const char& operator[](size_type i) const {                 return data[i];         }         size_type size() const {                 return length;         } private:         allocator<char> alloc;         char *data;         size_type length; };```
• 04-01-2012
laserlight
Quote:

Originally Posted by tap3ah
The whole reason I started this thread is that I badly needed help with some previous exercises, but I could find it only for few of them.

Based on results, your approach to getting help is not working.

Quote:

Originally Posted by tap3ah
The idea is to let people post what they think might be solutions, and the other, more knowledgeable people, correct them, comment and advise. Pretty much like every other forum.

I told you to post your own solutions because it sounded like you were just asking people to do your own homework for you. (It is homework even if you are not in school.) Realistically, people are not going to "post what they think might be solutions" unless they are actively working on those exercises right now and want feedback. Even then, they would probably start a new thread specific to their problem.

In response, you basically did a code dump with just a brief description of the exercise. If I wanted to help you, I would have to look up my copy of Accelerated C++ for the context (and what more if I didn't have a copy of Accelerated C++?), then give you a general code review. Not worth my time, so I passed.

If I you want help, be specific. Provide context (especially if the exercise is part of a larger series of exercises) and explain what you need help with. If you are looking for a code review, say so, and mention the specific areas that you would like your reviewers to critique. Check out the link in my signature for an article on how to ask smart questions.
• 04-01-2012
tap3ah
Quote:

Originally Posted by laserlight
I told you to post your own solutions because it sounded like you were just asking people to do your own homework for you.

You posted while I was typing my first solution. You were just too fast.

Quote:

Originally Posted by laserlight
Realistically, people are not going to "post what they think might be solutions" unless they are actively working on those exercises right now and want feedback. Even then, they would probably start a new thread specific to their problem.
If I wanted to help you, I would have to look up my copy of Accelerated C++ for the context (and what more if I didn't have a copy of Accelerated C++?), then give you a general code review. Not worth my time, so I passed.
If I you want help, be specific. Provide context (especially if the exercise is part of a larger series of exercises) and explain what you need help with.

This was aimed to people actively working on those exercises, or at least own the book. I thought it was obvious.
The whole context would be practically the whole chapter.

Quote:

Originally Posted by laserlight
If you are looking for a code review, say so, and mention the specific areas that you would like your reviewers to critique.

I'm sorry I didn't mention that. Again, I thought it was obvious.

Edit: Just a quote from E. Raymond's FAQ:
Quote:

Reply to a first offender off-line. There is no need of public humiliation for someone who may have made an honest mistake. A real newbie may not know how to search archives or where the FAQ is stored or posted.
• 04-01-2012
phantomotap
Quote:

I'm sure you see logic in your reasoning, but the truth is, you are just an elitist jerk who enjoys putting novice coders down.
Yep, that's why I'm taking the time to tell you how to learn and get actual help instead of just calling you an idiot.

Soma
• 04-01-2012
MK27
It is pretty obvious, tap3ah, that you have not made much use of programming forums before (or if you have, most of it involved flame wars, lol). Set your sensitive ego aside for a few minutes and try to think about what is being said to you.

Keep in mind that you can kick and scream about all this for as long as you want, but that is not going to change anything.

Also keep in mind that you will probably not find a better forum for C++ on the internet, and you certainly won't find a kinder one, before you get too into the kicking and screaming.

Quote:

Originally Posted by tap3ah
This was aimed to people actively working on those exercises, or at least own the book. I thought it was obvious.
The whole context would be practically the whole chapter.

Nothing wrong with trying, but evidently none of the forum regs are currently working on those exercises. There is always the chance that someone who owns the book may want to revisit this stuff, but I would consider you lucky if that happened. Again, nothing wrong with spinning the wheel and going for "lucky". But at some point, you may want to consider that you didn't get lucky, so...next step: realism.

Quote:

Originally Posted by tap3ah
Quote:

Originally Posted by laserlight
mention the specific areas that you would like your reviewers to critique

I'm sorry I didn't mention that. Again, I thought it was obvious.

It's always easy to see the point of something you wrote as "obvious". Assuming that it is just as obvious to someone who didn't write it, however, is sort of foolish, because it is rarely true.

So someone has now pointed out to you that it is not obvious what you want. But you did not do anything to resolve that.

Quote:

Edit: Just a quote from E. Raymond's FAQ: Reply to a first offender off-line. There is no need of public humiliation for someone who may have made an honest mistake. A real newbie may not know how to search archives or where the FAQ is stored or posted.
Do you mean this guy?

I'm sure that is not what he is talking about. Maybe you should have read the rest of that FAQ, eg:

http://catb.org/~esr/faqs/smart-ques...tml#not_losing

Quote:

Community standards do not maintain themselves: They're maintained by people actively applying them, visibly, in public. Don't whine that all criticism should have been conveyed via private e-mail: That's not how it works. Nor is it useful to insist you've been personally insulted when someone comments that one of your claims was wrong, or that his views differ. Those are loser attitudes.
Anyway, I've never seen laserlight humiliate anyone. Being asked to "mention the specific areas that you would like your reviewers to critique" is not humiliation, IT'S A TIP. If you want to find out what "humiliation" entails, keep up the pointless argument, I'm sure we'll get there quickly. Remember:

The harder they come, the harder they fall.
• 04-01-2012
tap3ah
Quote:

Originally Posted by MK27
It is pretty obvious, tap3ah, that you have not made much use of programming forums before (or if you have, most of it involved flame wars, lol).

Actually, I used dozens of technical forums without using a single harsh word.

Quote:

Originally Posted by MK27
Nothing wrong with trying, but evidently none of the forum regs are currently working on those exercises. There is always the chance that someone who owns the book may want to revisit this stuff, but I would consider you lucky if that happened. Again, nothing wrong with spinning the wheel and going for "lucky". But at some point, you may want to consider that you didn't get lucky, so...next step: realism.

Thank you for pointing that out. I would probably realize that myself after long period od inactivity of the thread.

Quote:

Originally Posted by MK27
So someone has now pointed out to you that it is not obvious what you want. But you did not do anything to resolve that.

Yes, I did. I said I was sorry.

Quote:

Originally Posted by MK27
Anyway, I've never seen laserlight humiliate anyone. Being asked to "mention the specific areas that you would like your reviewers to critique" is not humiliation, IT'S A TIP. If you want to find out what "humiliation" entails, keep up the pointless argument, I'm sure we'll get there quickly.

I was referring to phantomotap's first post where I was accused of bragging. No matter how I tried, I could find a shred of a reason why would anyone consider that bragging. That IS humiliating and you don't have to have especially sensitive ego to be irritated by that.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last