(冒泡,選擇,插入,希爾,快速,歸并,堆排) 周末加班學習C++,打算用C++寫七大經典排序代碼。 之前寫好的C實現代碼debug后運行良好。 之前的mysortlib.h中函數聲明如下,接口完全是一樣的。
void _bubsort(int arr[], int len);
void _bubsort_(int arr[], int len);void _isort(int arr[], int len);
void _isort_(int arr[], int len);void _ssort(int arr[], int len);
void _ssort_(int arr[], int len);void _selsort(int arr[], int len);void _msort(int arr[], int len);void _qsort_easy(int arr[], int len);
void _qsort(int arr[], int len);void _hsort(int arr[], int len);
?
我因此設計的C++接口是void sort_func(vector<int> &arr, int len); 考慮到:這樣調用也合理void sort_func(vector<int> &arr); 先試試用默認參數形式void sort_func(vector<int> &arr, int len=arr.size());錯誤提示:arr not in the context。 那么就用函數重載,函數名一樣,參數不同,每一個都定義兩個。 于是想要換個方法。想到宏。 測試是成功的,可是可讀性有一定損失。 首先,建立一個測試文件測試可行性。 ===================================================================== = 內容如下 =====================================================================
#include <iostream>
#include <vector>
#include <algorithm>/* 宏定義適配器(僅聲明) */
#define MACRO_VECTOR_INT_DECLARE(func_name_in_macro) void func_name_in_macro(std::vector<int> &arr, int len)
/* 宏定義適配器(僅聲明) */
#define MACRO_VECTOR_DECLARE(func_name_in_macro) void func_name_in_macro(std::vector<int> &arr)
/* 宏定義適配器 */
#define MACRO_VECTOR_IMPLEMENT(func_name_in_macro) MACRO_VECTOR_DECLARE(func_name_in_macro){func_name_in_macro(arr, arr.size());}using namespace std;/* 一開始想用這種方式實現批量聲明,定義函數,失敗。* arr not in the context !
void _bubsort(vector<int> &arr, int len=arr.size()){arr[0] = len;cout << len << endl;
}*/
MACRO_VECTOR_INT_DECLARE(_bubsort); // 聲明函數 _bubsort(std::vector<int> &arr, int len);MACRO_VECTOR_INT_DECLARE(_bubsort){ // 函數定義cout << "this function was created by macro with an argument funciton name! and this function use an argument len:" << len << endl;
}
MACRO_VECTOR_DECLARE(_bubsort); // 補充一個適配器 _bubsort(std::vector<int> &arr);
MACRO_VECTOR_IMPLEMENT(_bubsort) // 補充一個適配器 _bubsort(std::vector<int> &arr){ _bubsort(arr, arr.size()); }#define sortfunc _bubsortint print_int(int n){ cout << n << ' ' ; }
int main(int argc, char* argv[]){vector<int> arr(10);srand(time(0));generate(arr.begin(), arr.end(), rand);for_each(arr.begin(), arr.end(), print_int);cout << endl;sortfunc(arr, 10);cout << "that is diff call" << endl;sortfunc(arr);return 0;
}
?
編譯,運行,調試,最后成功。說明是可行的。 然后實際運用,將代碼分拆到幾個文件。 ===================================================================== = 聲明變成如下 =====================================================================
#ifndef _MYSORTLIB_H
#define _MYSORTLIB_H/* 宏定義適配器(僅聲明) */
#define MACRO_VECTOR_INT_DECLARE(func_name_in_macro) void func_name_in_macro(std::vector<int> &arr, int len)
/* 宏定義適配器(僅聲明) */
#define MACRO_VECTOR_DECLARE(func_name_in_macro) void func_name_in_macro(std::vector<int> &arr)
/* 宏定義適配器 */
#define MACRO_VECTOR_IMPLEMENT(func_name_in_macro) MACRO_VECTOR_DECLARE(func_name_in_macro){func_name_in_macro(arr, arr.size());}#include <vector>/* 接口相同,僅函數名不同,故批量定義 */MACRO_VECTOR_INT_DECLARE(_bubsort);
MACRO_VECTOR_INT_DECLARE(_bubsort_);MACRO_VECTOR_INT_DECLARE(_isort);
MACRO_VECTOR_INT_DECLARE(_isort_);MACRO_VECTOR_INT_DECLARE(_ssort);
MACRO_VECTOR_INT_DECLARE(_ssort_);MACRO_VECTOR_INT_DECLARE(_selsort);MACRO_VECTOR_INT_DECLARE(_msort);MACRO_VECTOR_INT_DECLARE(_qsort_easy);
MACRO_VECTOR_INT_DECLARE(_qsort);MACRO_VECTOR_INT_DECLARE(_hsort);/* --------------------------------------*/MACRO_VECTOR_DECLARE(_bubsort);
MACRO_VECTOR_DECLARE(_bubsort_);MACRO_VECTOR_DECLARE(_isort);
MACRO_VECTOR_DECLARE(_isort_);MACRO_VECTOR_DECLARE(_ssort);
MACRO_VECTOR_DECLARE(_ssort_);MACRO_VECTOR_DECLARE(_selsort);MACRO_VECTOR_DECLARE(_msort);MACRO_VECTOR_DECLARE(_qsort_easy);
MACRO_VECTOR_DECLARE(_qsort);MACRO_VECTOR_DECLARE(_hsort);#endif /*_MYSORTLIB_H*/
===================================================================== = 實現如下: =====================================================================
#define swap(a,b) {int tmp_that_must_be_unique=a; a=b; b=tmp_that_must_be_unique;}/* 適配器批量生產 */
MACRO_VECTOR_IMPLEMENT(_bubsort);
MACRO_VECTOR_IMPLEMENT(_bubsort_);MACRO_VECTOR_IMPLEMENT(_isort);
MACRO_VECTOR_IMPLEMENT(_isort_);MACRO_VECTOR_IMPLEMENT(_ssort);
MACRO_VECTOR_IMPLEMENT(_ssort_);MACRO_VECTOR_IMPLEMENT(_selsort);MACRO_VECTOR_IMPLEMENT(_msort);MACRO_VECTOR_IMPLEMENT(_qsort_easy);
MACRO_VECTOR_IMPLEMENT(_qsort);MACRO_VECTOR_IMPLEMENT(_hsort);/* 實際實現者 */
MACRO_VECTOR_INT_DECLARE(_bubsort){int i=len,j=0;while (i>1) {for (j=0;j<i-1;j++) {if (arr[j]>arr[j+1])swap(arr[j], arr[j+1]);}i--;}
}MACRO_VECTOR_INT_DECLARE(_bubsort_){
}MACRO_VECTOR_INT_DECLARE(_isort){
}
MACRO_VECTOR_INT_DECLARE(_isort_){
}MACRO_VECTOR_INT_DECLARE(_ssort){
}
MACRO_VECTOR_INT_DECLARE(_ssort_){
}MACRO_VECTOR_INT_DECLARE(_selsort){
}MACRO_VECTOR_INT_DECLARE(_msort){
}MACRO_VECTOR_INT_DECLARE(_qsort_easy){
}
MACRO_VECTOR_INT_DECLARE(_qsort){
}MACRO_VECTOR_INT_DECLARE(_hsort){
}
?
以上代碼均已測試通過。 **最后,現在為止,其實宏還是可以再合并精簡的。不過越是合并越是難讀。**
轉載于:https://www.cnblogs.com/weishun/p/tencent-shixi-2014-08-10-2.html
創作挑戰賽 新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔 為你收集整理的【实习记】2014-08-10(下)用宏来批量声明定义函数 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。