实用模板

来源:互联网 发布:微信刷票软件免费版 编辑:程序博客网 时间:2024/06/12 14:33

template<typename T>inlinevoid SafeDelete(T*&p)
{ delete p; p
= 0; }
template
<typename T>inlinevoid SafeDeleteArray(T*&p)
{ delete[] p; p
= 0; }

template
<typename T>inline voidUnusedVar(constT&)
{}

template
<typename T>inline size_t CountOf(constT& array)
{ UnusedVar(array);
return sizeof(array) / sizeof(array[0]); }

template
<typename T>inline constT& Max(const T&a, const T& b)
{
return a> b ? a : b; }

template
<typename T>inline constT& Min(const T&a, const T& b)
{
return a< b ? a : b; }

template
<typename T>inline voidSwap(T& a, T& b)
{ T t(a); a
= b; b = t; }

 

template<classT, size_t N>
inline size_t CountOf(
const T(&)[N])
{
   
return N;
}

//直接这么用就可以了:
int a[9];
char aa[10][11];

CountOf(a);
//返回9
CountOf(aa);//返回10


//判断参数类型是否是无符号类型,当然只限于有序类型
template<classT>
inline
bool IsUnsigned(T)
{
   
return ((T)-1> 0);//强制类型转换,不是相减,T是由实参推演出来的实参的类型

}
//按内存方式强制类型转换,如将type (CLS::*pf)(type par)强制转换为void *:  void *p = GetCast<void *>(pf);
template<classDEST, classSRC>
DEST GetCast(
const SRC& src)
{
    union
    {
        SRC  src;
        DEST dest;
    }myunion
= {src};

   
return myunion.dest;
}

//注意:请不要用于指针之间的转换,特别是BSTR
template<typename lType, typename rType>
lType TranslateType(rType
& rValue, IsDiff& isDiff)
{
    stringstream
is;
    lType lValue;

   
is << rValue;
   
is >> lValue;

   
return lValue;
};

#ifndef __SINGLE_H__
#define __SINGLE_H__

#include
<new>

template
<classT> class TSingle
{
public:
    inline
static T* Create()
    {
       
if(!spT_)
        {
            spT_
= new(&sMemory_) T;
        }
       
returnspT_;
    }

    inline
static T* Instance()
    {
       
returnspT_;
    }

    inline
static void Destroy()
    {
       
if(spT_) { spT_->~T();
            spT_
= 0;
        }
    }
private:
    typedef union
    {
       
chart_[sizeof(T)];
       
shortint shortInt_;
       
intint_;
       
longint longInt_;
       
floatfloat_;
       
doubledouble_;
       
longdouble longDouble_;
    }UNMaxAlign;

   
static UNMaxAlign sMemory_;
   
static T*         spT_;
};

template
<classT> typename
TSingle
<T>::UNMaxAlign TSingle<T>::sMemory_;
template
<classT> typename
T
* TSingle<T>::spT_= 0;
#endif //__SINGLE_H__

发一个快速排序模板
//交换数据template<typename T>void Swap(T&a, T& b) { T c(a); a = b; b = c; }//排序 template<typename T>void QuickSort(T*arr, int left, int right) {int l, r; T v; l= left; r = right; v = arr[(left + right) /2]; while (l <=r) { while (arr[l] < v)++l; while (arr[r] > v) --r;if (l <= r) { Swap<T>(arr[l], arr[r]);++l; --r; } } if(l == r) l++;if (left < r) QuickSort<T>(arr, left, r);if (l < right) QuickSort<T>(arr, l, right); }int main() {int i; int a[]={1,4,2,66,22,23,5,6,8,7,99,54}; QuickSort<int>(a,0, 11); for(i=0;i<12;i++) printf("%d\n",a[i]);double b[]={5.1,4.5,2.2,66.5,22.6,23,5,6,18.1,7,99,54}; QuickSort<double>(b,0, 11); for(i=0;i<12;i++) printf("%f\n",b[i]);return 0; }
 
//将元素累加到map
template<typename KEY, typename VALUE>
inline
void AddOneToMap(map<KEY, VALUE>&mp, const KEY& key,const VALUE&value)
{
    map
<KEY, VALUE>::iterator it= mp.find(key);
   
if (it != mp.end())
    {
        it
->second+= value;
    }
   
else
    {
        mp.insert(make_pair(key, value));
    }
}

0 0
原创粉丝点击