設計一個公共的class,通過它的接口可以對任何類型的數組排序



template<class T>
class Test
{
	public:
		static void Sort(T *array,int len,bool (*Compare)(T& a,T& b))//static 可以直接通過Test<T>::Sort訪問
		{
			T temp;
			ASSERT(len>=1);
			for (int i = 0;i < len-1;i++)//冒泡排序
			{
				for (int j = len-1;j>i;j--)
				{
					if (Compare(array[j],array[j-1]))//使用函數指針的方式進行比較
					{
						temp = array[j-1];//根據升序或降序進行交換
						array[j-1] = array[j];
						array[j] = temp;
					}
				}
			}
		}
};

template<class T>
bool ascend(T& a,T& b)
{
	return a < b?true:false;
}

template<class T>
bool descend(T& a,T& b)
{
	return a>b?true:false;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章