c/c++语言开发共享C++模板元编程实现选择排序

前言模板在c++一直是比较神秘的存在。 stl 和 boost 中都有大量运用模板,但是对于普通的程序员来说,模板仅限于使用。在一般的编程中,很少会有需要自己定义模板的情况。但是作为一个有理想的程序员


前言

模板在c++一直是比较神秘的存在。 stl 和 boost 中都有大量运用模板,但是对于普通的程序员来说,模板仅限于使用。在一般的编程中,很少会有需要自己定义模板的情况。但是作为一个有理想的程序员,模板是一个绕不过去的坎。由于c++标准的不断改进,模板的能力越来越强,使用范围也越来越广。

在c++11中,模板增加了 constexpr ,可变模板参数,回返类型后置的函数声明扩展了模板的能力;增加了外部模板加快了模板的编译速度;模板参数的缺省值,角括号和模板别名使模板的定义和使用变得更加的简洁。

c++14中,放宽了 constexpr 的限制,增加了变量模板。

c++17中,简化模板的构造函数,使模板更加易用;folding使得模板在定义中更加方便。

c++20是一个大版本更新,对于模板来说,也有很大的进步。对于个人来说,最喜欢的应该就是 concept 了,它让模板可以判断模板参数是不是符合要求,同时也对模板的特化提供了更进一部的支持(以后再也不用看着模板成吨的报错流泪了。);同时它还要求大部分的stl库都支持 constexpr ,使得很多类可以在编译期直接使用(以后模板元编程就不是单纯的函数式语言了吧,感觉以后c++的编程会变得非常奇怪)。

而随着模板一步步的完善,大佬们发现模板的功能居然已经实现了图灵完备,于是各种骚操作层出不穷,比如俄罗斯方块super template tetris 。

作为一个小老弟,当然是还没有能力写出一个可以媲美俄罗斯方块的程序,不过写一些简单的排序还是可以的。

这里我分享的是一个选择排序算法。为什么选择选择排序呢?因为它排序的时候,他对于元素的位置改变是比较少的。个人感觉函数元编程最复杂的就是对元素进行修改位置了吧。

代码详解

数据的结构

  template<int ...data>  struct mvector;    template<int first, int ...data>  struct mvector<first, data...> {   static constexpr int size = sizeof...(data) + 1;   static constexpr int value = first;   typedef mvector<data...> next_type;   constexpr static std::array<int, sizeof...(data) + 1> array = {first, data...};  };    template<int first>  struct mvector<first> {   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<int ...data>  struct mvector;  

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

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

数据的操作

分割向量

  // 分割向量  template<int index, typename t, typename s>  struct splitvector;    template<int index, int ...leftdata, int ...rightdata>  struct splitvector<index, mvector<leftdata...>, mvector<rightdata...>> {   typedef splitvector<index - 1, mvector<leftdata..., mvector<rightdata...>::value>, typename mvector<rightdata...>::next_type> next_split;   typedef typename next_split::leftvector leftvector;   typedef typename next_split::rightvector rightvector;  };    template<int ...leftdata, int ...rightdata>  struct splitvector<0, mvector<leftdata...>, mvector<rightdata...>> {   typedef mvector<leftdata...> leftvector;   typedef typename mvector<rightdata...>::next_type rightvector;  };    

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

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

输出有 leftvector (出来的左边), rightvector (出来的右边)。

合并向量

  // 合并向量  template<typename t, typename s>  struct mergevector;    template<int ...dataa, int ...datab>  struct mergevector<mvector<dataa...>, mvector<datab...>> {   typedef mvector<dataa..., datab...> result_type;  };    

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

寻找最大值

  template<int now_index, typename u, typename v>  struct findmax;    template<int now_index, int ...looped, int ...unlooped>  struct findmax<now_index, mvector<looped...>, mvector<unlooped...>> {   typedef findmax<now_index + 1, mvector<looped..., mvector<unlooped...>::value>, typename mvector<unlooped...>::next_type> next_max;   constexpr static int max = mvector<unlooped...>::value > next_max::max ? mvector<unlooped...>::value : next_max::max;   constexpr static int max_index = mvector<unlooped...>::value > next_max::max ? now_index : next_max::max_index;  };    template<int now_index, int ...looped>  struct findmax<now_index, mvector<looped...>, mvector<>> {   typedef findmax<now_index, mvector<looped...>, 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<typename t, typename s>  struct selectsortwork;    template<int ...unsorted, int ...sorted>  struct selectsortwork<mvector<unsorted...>, mvector<sorted...>> {   typedef findmax<0, mvector<>, mvector<unsorted...>> max_find_type;   constexpr static int max = max_find_type::max;   constexpr static int max_index = max_find_type::max_index;   typedef splitvector<max_index, mvector<>, mvector<unsorted...>> split_type;   typedef selectsortwork<typename mergevector<typename split_type::leftvector, typename split_type::rightvector>::result_type, mvector<max, sorted...>> next_select_sort_work_type;   typedef typename next_select_sort_work_type::sorted_type sorted_type;  };    template<int ...sorted>  struct selectsortwork<mvector<>, mvector<sorted...>> {   typedef mvector<sorted...> sorted_type;  };

总结

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

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

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

  • 如果熟悉函数式编程的话,再来学习模板元编程,对于其中的理解会更加的深刻,所以最好在开始准备学习之前,先学习一下函数式编程会比较好(虽然这个过程会非常的痛苦)。
  • 类模板可以看作是一个函数,有输入输出。输入是模板的参数,输出是模板里面的类型或者变量,这些输出也可以作为函数计算的中间变量,方便编码。
  • 模板元编程,一定要有耐心,特别是debug,会特别的难受

到此这篇关于c++模板元编程实现选择排序的文章就介绍到这了,更多相关c++ 选择排序内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/596321.html

(0)
上一篇 2021年5月8日
下一篇 2021年5月8日

精彩推荐