C++11编译期(模板元编程)快速排序算法

#include <iostream>
using namespace std;

template< int...vs >
struct array { using type = array< vs... >; };

template< typename L, typename M, typename R > struct _concat;
template< int...v1, int...v2, int...v3 >
struct _concat< array< v1... >
              , array< v2... >
              , array< v3... >
              > : array< v1..., v2..., v3... > {};

template< int x, int y >
using _le = typename conditional< (x <= y), true_type, false_type >::type;

template< int x, int y >
using _gt = typename conditional< (x > y), true_type, false_type >::type;

template< template< int x, int y > class F, typename L > struct _filter;
template< template< int x, int y > class F >
struct _filter< F, array<> > : array<> {};
template< template< int x, int y > class F, int v1 >
struct _filter< F, array< v1 > > : array<> {};
template< template< int x, int y > class F, int v1, int v2, int...vs >
struct _filter< F, array< v1, v2, vs... > >
        : _concat< typename conditional< F< v2, v1 >::value
                                       , array< v2 >
                                       , array<>
                                       >::type
                 , typename _filter< F, array< v1, vs... > >::type
                 , array<>
                 > {};

template< typename T > struct _quick_sort;
template<> struct _quick_sort< array<> > : array<> {};
template< int x > struct _quick_sort< array< x > > : array< x > {};
template< int x, int...xs >
struct _quick_sort< array< x, xs... > > 
        : _concat< typename _quick_sort< typename _filter< _le
                                                         , array< x, xs... >
                                                         >::type
                                       >::type
                 , array < x >
                 , typename _quick_sort< typename _filter< _gt
                                                         , array< x, xs... >
                                                         >::type
                                       >::type
                 > {};

template< int...vs >
void print_array( array< vs... > ) {
    static constexpr int arr[sizeof...(vs)] = { vs... };
    for (size_t i = 0; i < sizeof...(vs); ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int main() {
    using input = array< 22, 4, 94, 70, 61, 65, 45, 95, 48, 88,
                         0, 35, 96, 62, 18, 72, 54, 10, 77, 76,
                         64, 26, 41, 20, 36, 58, 43, 25, 2, 78,
                         87, 40, 86, 21, 17, 99, 15, 28, 46, 31,
                         75, 39, 33, 60, 44, 19, 32, 91, 92, 74,
                         5, 83, 29, 79, 100, 55, 81, 23, 71, 42,
                         12, 49, 63, 30, 85, 97, 9, 69, 57, 1,
                         93, 3, 50, 80, 51, 8, 34, 27, 56, 98,
                         89, 38, 52, 16, 13, 6, 47, 59, 68, 7,
                         82, 24, 66, 90, 11, 84, 73, 37, 14, 67 >;
    using result = _quick_sort< input >;
    print_array(result{});
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个使用C++模板编程实现的编译器级别的快速排序: ```cpp #include <iostream> template <int... Args> struct IntList {}; template <typename List> struct Length; template <int Head, int... Tail> struct Length<IntList<Head, Tail...>> { static const int value = 1 + Length<IntList<Tail...>>::value; }; template <> struct Length<IntList<>> { static const int value = 0; }; template <typename List, int N> struct Get; template <int Head, int... Tail, int N> struct Get<IntList<Head, Tail...>, N> { static const int value = Get<IntList<Tail...>, N - 1>::value; }; template <int Head, int... Tail> struct Get<IntList<Head, Tail...>, 0> { static const int value = Head; }; template <typename List, int N> struct Set; template <int Head, int... Tail, int N> struct Set<IntList<Head, Tail...>, N> { using type = IntList<Head, typename Set<IntList<Tail...>, N - 1>::type>; }; template <int Head, int... Tail> struct Set<IntList<Head, Tail...>, 0> { using type = IntList<0, Tail...>; }; template <typename List, int N, int V> struct Insert; template <int Head, int... Tail, int N, int V> struct Insert<IntList<Head, Tail...>, N, V> { using type = typename Insert<typename Set<IntList<Head, Tail...>, N>::type, N - 1, V>::type; }; template <int V, int... Tail> struct Insert<IntList<Tail...>, 0, V> { using type = IntList<V, Tail...>; }; template <typename List> struct QuickSort; template <int Head, int... Tail> struct QuickSort<IntList<Head, Tail...>> { using smaller = typename QuickSort<typename Insert<IntList<Tail...>, 0, Head>::type>::type; using larger = typename QuickSort<typename Set<IntList<Tail...>, 0>::type>::type; using type = typename Insert<smaller, Length<smaller>::value, Head>::type; }; template <> struct QuickSort<IntList<>> { using type = IntList<>; }; int main() { using list = IntList<4, 7, 1, 9, 2, 8, 3, 6, 5>; using sorted_list = QuickSort<list>::type; std::cout << "Sorted list: "; for (int i = 0; i < Length<sorted_list>::value; ++i) { std::cout << Get<sorted_list, i>::value << " "; } std::cout << std::endl; return 0; } ``` 这个实现使用了C++11可变参数模板和递归模板特化来实现一个编译快速排序算法。它使用IntList模板来表示整数列表,Length模板用于计算列表的长度,Get模板用于获取列表中的素,Set模板用于设置列表中的素,Insert模板用于将素插入到列表中的指定位置,QuickSort模板用于实现快速排序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值