读书人

!关于模板类

发布时间: 2012-03-01 10:25:46 作者: rapoo

求助!关于模板类!
刚学习C++,编了一个堆的模板类,想以后用着方便,编译和连接都通过了,就是运行没有output,找不到错误,把代码贴出,向各位达人求救!

C/C++ code
template<typename _TY, typename _C = vector<_TY> >class heap{public:    typedef _C::allocator_type allocator_type;    typedef _C::value_type value_type;    typedef _C::size_type size_type;    explicit heap(const allocator_type& _Al = allocator_type())        : c(_Al) {}    allocator_type get_allocator() const        {return (c.get_allocator()); }    bool empty() const         {return c.empty(); }    size_type size() const         {return c.size(); }    size_type parent(size_type i)        {return i >> 1; }    size_type left(size_type i)        {return i << 1; }    size_type right(size_type i)        {return i<<1 + 1; }    void push_back(const value_type &X)        {c.push_back(X); }    void build_maxheap();    void heapsort();    value_type maximum() const        {return c[0]; }    void increase_key(size_type, value_type);    size_type extract_max();    void insert(value_type);    value_type operator[](const size_type index)        {return c[index]; }//    value_type operator[](const size_type index) const//        {return c[index]; }private:    _C c;    void MaxHeapify(size_type, size_type);};template<typename _TY, typename _C>void heap<_TY, _C>::MaxHeapify(size_type i, size_type length){    size_type l = left(i);    size_type r = right(i);    size_type largest;    if((l < length) &&(c[l]>c[i]))        largest = l;    else        largest = i;    if((r < length) &&(c[r]>c[largest]))        largest = r;    if(largest != i)    {        value_type temp = c[i];        c[i] = c[largest];        c[largest] = temp;        MaxHeapify(largest, length);    }}template<typename _TY, typename _C>void heap<_TY, _C>::build_maxheap(){    for(size_type i = c.size()/2; i >= 0; --i)        MaxHeapify(i, c.size());}template<typename _TY, typename _C>void heap<_TY, _C>::heapsort(){    build_maxheap();    value_type temp;    for(size_type i = c.size()-1; i > 0; --i)    {        temp = c[0];        c[0] = c[i];        c[i] = temp;        MaxHeapify(0, i);    }}


[解决办法]
C/C++ code
#include <iostream>#include <vector>#include <ctime>#include <cstdlib>using namespace std;template<typename _TY, typename _C = vector<_TY> >class heap{public:    typedef _C::allocator_type allocator_type;    typedef _C::value_type value_type;    typedef _C::size_type size_type;    explicit heap(const allocator_type& _Al = allocator_type())        : c(_Al) {}    allocator_type get_allocator() const        {return (c.get_allocator()); }    bool empty() const         {return c.empty(); }    size_type size() const     {return c.size(); }    size_type parent(size_type i) //你的这里错了..注意思考一下    {return (i - 1)>> 1; }    size_type left(size_type i)    {return (i << 1)+1; }    size_type right(size_type i)    {return (i<<1) + 2; }    void push_back(const value_type &X)    {c.push_back(X); }    void build_maxheap();    void heapsort();    value_type maximum() const    {return c[0]; }    void increase_key(size_type, value_type);    size_type extract_max();    void insert(value_type);        value_type operator[](const size_type index)    {return c[index]; }    //    value_type operator[](const size_type index) const    //        {return c[index]; }private:    _C c;    void MaxHeapify(size_type, size_type);    };template<typename _TY, typename _C>void heap<_TY, _C>::MaxHeapify(size_type i, size_type length){    size_type l = left(i);    size_type r = right(i);    size_type largest;    if((l < length) &&(c[l]>c[i]))        largest = l;    else        largest = i;    if((r < length) &&(c[r]>c[largest]))        largest = r;    if(largest != i)    {        value_type temp = c[i];        c[i] = c[largest];        c[largest] = temp;        MaxHeapify(largest, length);    }    }template<typename _TY, typename _C>void heap<_TY, _C>::build_maxheap(){    for(size_type i = c.size()/2; i < 0x80000000; --i) // i 为unsigned 这个条件为永真!!!        MaxHeapify(i, c.size());}template<typename _TY, typename _C>void heap<_TY, _C>::heapsort(){    build_maxheap();    value_type temp;    for(size_type i = c.size()-1; i > 0; --i)    {        temp = c[0];        c[0] = c[i];        c[i] = temp;        MaxHeapify(0, i);    }}int main(){    heap<int> k;    srand(time(0));    for (int i = 0; i < 100; ++i)        k.push_back(rand()%1000);    k.heapsort();    for (int j = 0; j < 100; ++j)        cout << k[j] << '\t';    return 0;} 

读书人网 >C++

热点推荐