Loading

C++模板元编程----选择排序

目录

前言

模板在C++一直是比较神秘的存在。STLBoost中都有大量运用模板,但是对于普通的程序员来说,模板仅限于使用。在一般的编程中,很少会有需要自己定义模板的情况。但是作为一个有理想的程序员,模板是一个绕不过去的坎。由于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;

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

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

数据的操作

分割向量

// 分割向量
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_indexLooped(已经比较的部分),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上,select_sort.cpp

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

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

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

博客原文:https://www.cnblogs.com/ink19/p/cpp_template_select_sort.html

posted @ 2020-12-12 16:21  ink19  阅读(649)  评论(0编辑  收藏  举报