Like Tree5Likes

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

This is a discussion on Exercise solutions from "Accelerated C++" (Koenig/Moo) within the C++ Programming forums, part of the General Programming Boards category; Post your own solutions, or comment those posted by others....

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    12

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

    Post your own solutions, or comment those posted by others.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    If you want a critique, post your own solutions
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    12

    [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;
      }
    }

  4. #4

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    12

    [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;
    }

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,246
    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

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    12
    Quote Originally Posted by phantomotap View Post
    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.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,246
    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

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    12
    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.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    12

    [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;
    };
    Last edited by tap3ah; 04-01-2012 at 05:52 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    12
    Quote Originally Posted by laserlight View Post
    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 View Post
    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 View Post
    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:
    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.

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,246
    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

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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 View Post
    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.

    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?

    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

    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.
    Last edited by MK27; 04-01-2012 at 05:09 PM.
    jamesallen4u likes this.
    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

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    12
    Quote Originally Posted by MK27 View Post
    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 View Post
    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 View Post
    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 View Post
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me with this "prime checker" exercise!
    By blackhawk65 in forum C++ Programming
    Replies: 3
    Last Post: 07-15-2010, 01:09 PM
  2. Replies: 2
    Last Post: 05-02-2010, 01:49 AM
  3. Few questions from "Accelerated C++"
    By s_siouris in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2008, 02:07 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21