溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

利用C++模板元編程怎么實現(xiàn)一個選擇排序功能

發(fā)布時間:2020-12-14 14:32:00 來源:億速云 閱讀:168 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹利用C++模板元編程怎么實現(xiàn)一個選擇排序功能,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

數(shù)據(jù)的結(jié)構(gòu)

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 模板,他的作用就是用來保存數(shù)據(jù)的。模板的原型是

template<int ...data>
struct mvector;

他可以輸入任意數(shù)量的整數(shù)(模板參數(shù)可以看作是輸入)。

根據(jù)后面的特化,模板一共有四個屬性或類型(這些可以看作是模板的輸出),分別是 size , value (第一個元素的值,方便后面的迭代), next_type (除去頭的尾部,方便迭代), array ( mvector 的數(shù)組表現(xiàn)形式)。

數(shù)據(jù)的操作

分割向量

// 分割向量
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 (已經(jīng)比較的部分), unLooped (未比較的部分)。其中 now_index 是多余的,可以使用 sizeof...(Looped) 來代替。

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

排序

對數(shù)據(jù)操作完成了,這個程序也就完成了一大半了,排序也是非常的簡單,從未排序的列表中,選擇最大的值,放到已經(jīng)排序好的列表的前面就好了。

// 排序
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;
};

總結(jié)

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

總的來說,代碼還是非常的簡單的,只要合理的進行分解,大部分的算法應(yīng)該都是可以實現(xiàn)的。

在編程的過程中,我也有一些自己的領(lǐng)悟,對于模板元編程的幾點小Tips,在這里給大家介紹一下吧。

  • 如果熟悉函數(shù)式編程的話,再來學(xué)習(xí)模板元編程,對于其中的理解會更加的深刻,所以最好在開始準備學(xué)習(xí)之前,先學(xué)習(xí)一下函數(shù)式編程會比較好(雖然這個過程會非常的痛苦)。

  • 類模板可以看作是一個函數(shù),有輸入輸出。輸入是模板的參數(shù),輸出是模板里面的類型或者變量,這些輸出也可以作為函數(shù)計算的中間變量,方便編碼。

  • 模板元編程,一定要有耐心,特別是debug,會特別的難受

關(guān)于利用C++模板元編程怎么實現(xiàn)一個選擇排序功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI