Towers of Hanoi redux Inspired by Mayur Kulkarni's question this morning, I decided to post some code for the classic Towers of Hanoi problem, but this time in C++. Since this is fairly "textbook" kind of task, all suggestions are welcome.

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;iterator&gt;

class Hanoi {
    typedef std::vector&lt;int&gt; Tower;
    Tower a, b, c;

    void show(std::string const &amp;label, Tower c) {
        std::cout &lt;&lt; label;
        std::copy(c.cbegin(), c.cend(), std::ostream_iterator&lt;int&gt;(std::cout, "\t"));
        std::cout &lt;&lt; "\n";
    }

    void dump() {
        show("A: ", a);
        show("B: ", b);
        show("C: ", c);
        std::cout &lt;&lt; "\n";
    }

    void MoveDisc(Tower &amp;a, Tower &amp;b) {
        b.push_back(a.back());
        a.pop_back();
    }

    void MoveStack(Tower &amp;a, Tower &amp;b, Tower &amp;c, size_t size) {
        if (size == 0)
            return;
        MoveStack(a, c, b, size - 1);
        MoveDisc(a, b);
        dump();
        MoveStack(c, b, a, size - 1);
    }
public:
    Hanoi(std::initializer_list&lt;int&gt; input) : a(input) { dump(); }

    void operator()() { MoveStack(a, c, b, a.size()); }
};

int main() {
    Hanoi h { 30, 25, 20, 15, 10, 5 };
    h();    
}
Here are just a few observations that may help you improve your code.  In all, it is quite clean and easy to read and understand.

Use const where practical

The show() and dump() function don't (and shouldn't) alter the underlying object, so they should be declared const:

void show(std::string const &amp;label, Tower c) const;
void dump() const;


Use reserve() to prevent reallocations

The constructor can easily determine the maximum size of the vectors.  If there were much larger ones than this, one could avoid potentially costly memory reallocations by using Tower.reserve() within the constructor.

Prefer flexible output

This is admittedly a very simple program, but generally it's nice to allow for printing to any std::ostream rather than having std::cout hardcoded into the class.  Perhaps it could be a constructor argument.

Use all required #includes

The code makes use of std::initializer_list but doesn't have this line:

#include &lt;initializer_list&gt;


However, as @Morwenn points out in a comment, it's not needed to include it explicitly since std::vector must necessarily already include it.