1. 程式人生 > 程式設計 >C++模板超程式設計實現選擇排序

C++模板超程式設計實現選擇排序

前言

模板在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<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<>> {
 typedef FindMax<now_index,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<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,在這裡給大家介紹一下吧。

  • 如果熟悉函數語言程式設計的話,再來學習模板超程式設計,對於其中的理解會更加的深刻,所以最好在開始準備學習之前,先學習一下函數語言程式設計會比較好(雖然這個過程會非常的痛苦)。
  • 類模板可以看作是一個函式,有輸入輸出。輸入是模板的引數,輸出是模板裡面的型別或者變數,這些輸出也可以作為函式計算的中間變數,方便編碼。
  • 模板超程式設計,一定要有耐心,特別是debug,會特別的難受

到此這篇關於C++模板超程式設計實現選擇排序的文章就介紹到這了,更多相關C++ 選擇排序內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!