class People
{
public  :
    People(int iage,char *n)
    {
        age = iage ;
        strcpy(name,n) ;
    }
    bool operator >(const People& m) const
    {
        return( (this->age) >  m.getage() ) ;
    }
    int getage() const {return age;}
    const char* getname() const {return name;}
private :
    int age ;
    char name[28] ;
} ;

int main()
{

    vector<People> v1 ;
    vector<People> v2 ;
    vector<People> v3 ;
    vector<vector<People> > vx ;
    char namex[3][20] = {"People1","People2","People3"} ;

    for(int idx=0;idx<30;idx++)
    {
        if( (idx%3)==0)
            v1.emplace_back(idx,namex[0]) ;
        else if( (idx%3)==1)
            v2.emplace_back(idx,namex[1]) ;
        else
            v3.emplace_back(idx,namex[2]) ;
    }//for

    vx.push_back(v1) ;
    vx.push_back(v2) ;
    vx.push_back(v3) ;

    cout << "ready " << endl ;

    vector<People> v = merge_arrays<People>(vx) ;

    cout << "done " << endl ;

    char tmp[10] = "12345" ;
    People iret(0,tmp) ;
    while(!v.empty())
    {
        iret = v.back() ;
        v.pop_back() ;
        cout << iret.getage() << " " << iret.getname() << endl ;
    } //while
    cout << endl ;

}

 

template <typename T>
class Compare{
public:
    bool operator()(const pair<T,int> &lhs,const pair<T,int> &rhs) const{
        return lhs.first > rhs.first ;
        //T t1 = lhs.first ;
        //T t2 = rhs.first ;
        //return (t1 > t2) ;
    }
} ;

template <typename T>
vector<T> merge_arrays(const vector<vector<T> > &S)
{
    priority_queue<pair<T,int>,vector<pair<T,int> >,Compare<T> > min_heap;

    vector<int> S_idx(S.size(),0) ;

    for(int i=0;i<S.size();++i)
    {
        min_heap.emplace(S[i][0],i) ;
        S_idx[i] = 1 ;
    } //for

    vector<T> ret ;
    while(!min_heap.empty())
    {
        pair<T,int> p = min_heap.top() ;

        ret.emplace_back(p.first) ;
        if(S_idx[p.second] < S[p.second].size())
        {
            min_heap.emplace(S[p.second][S_idx[p.second]++],p.second) ;
        }
        min_heap.pop() ;
    } //while


    return ret ;
}

 

創作者介紹
創作者 hedgezzz 的頭像
hedgezzz

hedgezzz的部落格

hedgezzz 發表在 痞客邦 留言(0) 人氣()