Post your own solutions, or comment those posted by others.
Post your own solutions, or comment those posted by others.
If you want a critique, post your own solutions
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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(); } // inlineCode: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; } }
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; }
O_o
This thread is stupid.
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
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.
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
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.
Last edited by tap3ah; 04-01-2012 at 05:07 AM.
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; };
Last edited by tap3ah; 04-01-2012 at 05:52 AM.
Based on results, your approach to getting help is not working.Originally Posted by tap3ah
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.Originally Posted by tap3ah
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You posted while I was typing my first solution. You were just too fast.
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.
I'm sorry I didn't mention that. Again, I thought it was obvious.
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.
Last edited by tap3ah; 04-01-2012 at 07:59 AM.
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.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.
Soma
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.
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.
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.Originally Posted by tap3ah
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.
Do you mean this guy?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.
Eric S. Raymond's Home Page
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
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: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.
The harder they come, the harder they fall.
Last edited by MK27; 04-01-2012 at 05:09 PM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Actually, I used dozens of technical forums without using a single harsh word.
Thank you for pointing that out. I would probably realize that myself after long period od inactivity of the thread.
Yes, I did. I said I was sorry.
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.