可以根據T* beg裡面的資料來排序std::vector<T*> depVector中的所有資料。
可以根據這個方法做出radix sort。

template <class T> void DependenceSort(T* beg, const uint total, std::vector<T*> depVector );

template <class T>
struct ptr_value_cmp
    : public std::binary_function<T, T, bool>
{    // functor for operator<
    bool operator()(T* _Left, T* _Right) const
    {    // apply operator< to operands
        return (*_Left) < (*_Right);
    }
};

template <class T>
void DependenceSort( T* beg, const uint total, std::vector<T*> depVector )
{
    T **ppAry = new T*[total];
    for (uint i=0;i<total;i++)
    {
        ppAry[i] = beg+i;
    }
    std::stable_sort(ppAry, ppAry+total, ptr_value_cmp<T>());
    uint *iAry = new uint[total];
    for (uint i=0;i<total;i++)
    {
        iAry[i] = ppAry[i]-beg;
    }
    T *Ary = new T[total];
    for (uint num=0;num<depVector.size();num++)
    {
        std::copy(depVector[num], depVector[num]+total, Ary);
        T* dst = depVector[num];
        for (uint i=0;i<total;i++)
        {
            dst[i] = Ary[iAry[i]];
        }
    }
    delete[] Ary;
    delete[] iAry;
    delete[] ppAry;
}

最近一直看SJC的源碼,也不知道老師從哪挖來的,總之覺得寫的超爛的,一直把using namespace std;放外面,然後一直衝名= =
總之一直看爛code心情會不好,不過也不想重構了,應該會在暑假重寫。

arrow
arrow
    全站熱搜

    讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()