利用C++模板元编程怎么实现一个选择排序功能
创始人
2024-12-18 22:21:12
0次

数据的结构

template
struct mvector;

template
struct mvector {
 static constexpr int size = sizeof...(data) + 1;
 static constexpr int value = first;
 typedef mvector next_type;
 constexpr static std::array array = {first, data...};
};

template
struct mvector {
 static constexpr int size = 1;
 static constexpr int value = first;
 typedef mvector<> next_type;
 constexpr static int array[] = {first};
};

template<>
struct mvector<> {
 static constexpr int size = 0;
 static constexpr int value = -1;
 typedef mvector<> next_type;
 constexpr static int array[] = {};
};

这里我们定义了一个 mvcetor 模板,他的作用就是用来保存数据的。模板的原型是

template
struct mvector;

他可以输入任意数量的整数(模板参数可以看作是输入)。

根据后面的特化,模板一共有四个属性或类型(这些可以看作是模板的输出),分别是 size , value (第一个元素的值,方便后面的迭代), next_type (除去头的尾部,方便迭代), array ( mvector 的数组表现形式)。

数据的操作

分割向量

// 分割向量
template
struct SplitVector;

template
struct SplitVector, mvector> {
 typedef SplitVector::value>, typename mvector::next_type> next_split;
 typedef typename next_split::LeftVector LeftVector;
 typedef typename next_split::RightVector RightVector;
};

template
struct SplitVector<0, mvector, mvector> {
 typedef mvector LeftVector;
 typedef typename mvector::next_type RightVector;
};

这个模板的主要目的是将向量从某一部分分离出来(取最大值)。

模板的输入有三个: index (要分离的元素的位置在 RightData 的位置), LeftData (分离的左边), RightData (分离的右边)。

输出有 LeftVector (出来的左边), RightVector (出来的右边)。

合并向量

// 合并向量
template
struct MergeVector;

template
struct MergeVector, mvector> {
 typedef mvector result_type;
};

将两个向量合并,主要是用在分割后的向量。

寻找最大值

template
struct FindMax;

template
struct FindMax, mvector> {
 typedef FindMax::value>, typename mvector::next_type> next_max;
 constexpr static int max = mvector::value > next_max::max ? mvector::value : next_max::max;
 constexpr static int max_index = mvector::value > next_max::max ? now_index : next_max::max_index;
};

template
struct FindMax, mvector<>> {
 typedef FindMax, mvector<>> next_max;
 constexpr static int max = -1;
 constexpr static int max_index = now_index;
};

寻找向量中的最大值。输入有 now_index , Looped (已经比较的部分), unLooped (未比较的部分)。其中 now_index 是多余的,可以使用 sizeof...(Looped) 来代替。

输出是 max (最大值), max_index (最大值的位置,方便后面的分割)

排序

对数据操作完成了,这个程序也就完成了一大半了,排序也是非常的简单,从未排序的列表中,选择最大的值,放到已经排序好的列表的前面就好了。

// 排序
template
struct SelectSortWork;

template
struct SelectSortWork, mvector> {
 typedef FindMax<0, mvector<>, mvector> max_find_type;
 constexpr static int max = max_find_type::max;
 constexpr static int max_index = max_find_type::max_index;
 typedef SplitVector, mvector> split_type;
 typedef SelectSortWork::result_type, mvector> next_select_sort_work_type;
 typedef typename next_select_sort_work_type::sorted_type sorted_type;
};

template
struct SelectSortWork, mvector> {
 typedef mvector sorted_type;
};

总结

代码我放在了github的gist上, select_sort.cpp 。

总的来说,代码还是非常的简单的,只要合理的进行分解,大部分的算法应该都是可以实现的。

在编程的过程中,我也有一些自己的领悟,对于模板元编程的几点小Tips,在这里给大家介绍一下吧。

  • 如果熟悉函数式编程的话,再来学习模板元编程,对于其中的理解会更加的深刻,所以最好在开始准备学习之前,先学习一下函数式编程会比较好(虽然这个过程会非常的痛苦)。

  • 类模板可以看作是一个函数,有输入输出。输入是模板的参数,输出是模板里面的类型或者变量,这些输出也可以作为函数计算的中间变量,方便编码。

  • 模板元编程,一定要有耐心,特别是debug,会特别的难受

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...