fork download
#include 
#include          // ptrdiff_t
#include       // std::remove_cv
#include           // std::move
#include            // std::vector

typedef ptrdiff_t Size;

template< class Type >
auto movable( Type& o )
    -> typename std::remove_cv::type&&
{
    typedef typename std::remove_cv::type NcvType;
    return std::move( const_cast( o ) );
}

namespace hare {

    // A non-copyable array.
    template< class Item >
    class NcArray
    {
    private:
        NcArray( NcArray const& );                  // No such.
        NcArray& operator=( NcArray const& );       // No such.

        std::vector   items_;

    public:
        typedef typename std::vector::const_iterator Iterator;

        void append( Item v ) { items_.emplace_back( movable( v ) ); }

        auto begin() const -> Iterator { return items_.begin(); }
        auto end() const -> Iterator { return items_.end(); }

        NcArray(): items_() {}

        NcArray( Item const& v )
            : items_( v, 1 )
        {}

        NcArray( NcArray&& other )
            : items_( move( other.items_ ) )
        {}
    };

    template< class Item >
    inline auto concat( NcArray a, Item const v )
        -> NcArray
    {
        a.append( movable( v ) );
        return movable( a );
    }

}  // namespace hare

typedef hare::NcArray Array;

auto collatz_aux( int const n, Array const a )
    -> Array
{
    if( n == 1 )
    {
        return concat( movable( a ), 1 );
    }
    else if( n % 2 == 0 )
    {
        return collatz_aux( n / 2, concat( movable( a ), n ) );
    }
    else
    {
        return collatz_aux( 3 * n + 1, concat( movable( a ), n ) );
    }
}

auto collatz( int const n )
    -> Array
{
    return collatz_aux( n, Array() );
}

auto main()
    -> int
{
    using namespace std;

    for( int const v : collatz( 5 ) )
    {
        cout << v << " ";
    }
    cout << endl;
}
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
5 16 8 4 2 1