(转载)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]);  
    }  
}   

猜你喜欢

转载自blog.csdn.net/qq_38365116/article/details/80184566