(转载)stl中list的sort算法实现

https://blog.csdn.net/qq276592716/article/details/7932483

list不能使用STL算法sort(),必须使用自己的sort()member functiion,因为STL算法sort()只接受Ramdon Access Iterator.

template <class _Tp, class _Alloc>  
void list<_Tp, _Alloc>::sort()  
{  
    // Do nothing if the list has length 0 or 1.  
        if (_M_node->_M_next != _M_node && _M_node->_M_next->_M_next != _M_node) {  
        list<_Tp, _Alloc> __carry;  
        list<_Tp, _Alloc> __counter[64];  
        int __fill = 0;  
        while (!empty()) {  
            __carry.splice(__carry.begin(), *this, begin());  
            int __i = 0;  
            while(__i < __fill && !__counter[__i].empty()) {  
                __counter[__i].merge(__carry);  
                __carry.swap(__counter[__i++]);  
            }  
            __carry.swap(__counter[__i]);        
            if (__i == __fill) ++__fill;  
        }   
        for (int __i = 1; __i < __fill; ++__i)  
            __counter[__i].merge(__counter[__i-1]);  
        swap(__counter[__fill-1]);  
    }  
}   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值