c++-模板元编程 编译期插入排序

前置课程
c++模板元编程TypeList定义及基本操作

#include <iostream>
#include <utility>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <boost/type_index.hpp>

using namespace std;

#include "def.h"
// 辅助类
template<typename T>
struct Identity {
    using type = T;
};


template<typename List, template<class, class> typename Compare, typename Elem, bool = IsEmpty_v<List>>
struct InsertSort_;

template<typename List, template<class, class> typename Compare, typename Elem>
using InsertSort_t = typename InsertSort_<List, Compare, Elem>::type;

template<typename List, template<class, class> typename Compare, typename Elem>
struct InsertSort_<List, Compare, Elem, false> {
 private:
    using Front = Front_t<List>;
    using NewTail = typename conditional_t<Compare<Elem, Front>::value, Identity<List>, InsertSort_<PopFront_t<List>, Compare, Elem>>::type;
    using NewHead = conditional_t<Compare<Elem, Front>::value, Elem, Front>;
 public:
    using type = PushFront_t<NewTail, NewHead>;
};

template<typename List, template<class, class> typename Compare, typename Elem>
struct InsertSort_<List, Compare, Elem, true> {
    using type = PushFront_t<List, Elem>;
};


template<typename List, template<class, class> typename Compare, bool = IsEmpty_v<List>>
 struct InsertionSort_ {
    using NewTail = typename InsertionSort_<PopFront_t<List>, Compare>::type;
    using NewHead = Front_t<List>;
    using type = InsertSort_t<NewTail, Compare, NewHead>;
};

template<typename List, template<class, class> typename Compare>
struct InsertionSort_<List, Compare, true> {
    using type = List;
};

template<typename List, template<class, class> typename Compare>
using InsertionSort_t = typename InsertionSort_<List, Compare>::type;

struct Big {
    char buffer[1024];
};

template<typename T, typename U>
struct LessBigger {
    constexpr static auto value = sizeof(T) < sizeof(U);
};

int main() {
    using SL = TypeList<long long, Big, bool, char, int, short>;
    using Sorted_SL = InsertionSort_t<SL, LessBigger>;
    cout << boost::typeindex::type_id_with_cvr<Sorted_SL>().pretty_name() << endl;
}


输出
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是一个使用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模板用于实现快速排序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值