算法公共头文件

/*
 * Copyright (c) 2007 Lotomer
 * 作    者  : Lotomer
 * 文 件 名  : common.h
 * 创建时间  : 2007.4.12
 * 说    明  : 排序算法的公共部分
 */
/
#ifndef SORT_DEBUG
    #define SORT_DEBUG
#endif
#ifdef SORT_DEBUG
#include <iostream>
using std::cout;
using std::endl;
template<class Elem>
void Print(Elem arr[], int nStart, int nEnd)
{   
    for (int i = nStart; i <= nEnd; ++i)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}
#endif  /*  ifdef SORT_DEBUG    */
//元素比较模板类
template<class T>
class Compare{
public:
 virtual bool operator()(T& t1, T& t2) = 0;
};
//继承模板类
//小于
template<class T>
class Lower : public Compare<T>{
public:
 bool operator()(T& t1, T& t2)
 {
  return t1 < t2;
 }
};
//大于
template<class T>
class Greater : public Compare<T>{
public:
 bool operator()(T& t1, T& t2)
 {
  return t1 > t2;
 }    
};
//-------------------------------------------------------
//交换元素
template<class Elem>
inline void swap(Elem arr[], int index_a, int index_b)
{
 Elem tmp = arr[index_a];
 arr[index_a] = arr[index_b];
 arr[index_b] = tmp; 
}
//重载
template<class Elem>
inline void swap(Elem &a, Elem &b)
{
    Elem tmp = a;
    a = b;
    b = tmp;
}
//部分特化
template<class Elem>
inline void swap(Elem* a, Elem* b)
{
    Elem* tmp = a;
    a = b;
    b = a;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值