实用模板
来源:互联网 发布:微信刷票软件免费版 编辑:程序博客网 时间: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])
{
}
//直接这么用就可以了:
int a[9];
char aa[10][11];
CountOf(a);//返回9
CountOf(aa);//返回10
//判断参数类型是否是无符号类型,当然只限于有序类型
template<classT>
inline bool IsUnsigned(T)
{
}
//按内存方式强制类型转换,如将type (CLS::*pf)(type par)强制转换为void *:
template<classDEST, classSRC>
DEST GetCast(const SRC& src)
{
}
//注意:请不要用于指针之间的转换,特别是BSTR
template<typename lType, typename rType>
lType TranslateType(rType& rValue, IsDiff& isDiff)
{
};
#ifndef __SINGLE_H__
#define __SINGLE_H__
#include <new>
template <classT> class TSingle
{
public:
};
template
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
{
it
}
{
mp.insert(make_pair(key, value));
}
}
- 实用模板
- 可行性分析,实用模板。
- 实用:源代码注释模板
- ACM 实用秘籍 模板
- SSH整合实用模板
- Pascal实用代码模板
- 二分查找实用模板
- 二分查找实用模板
- Linux iptables防火墙实用模板
- Linux iptables防火墙实用模板
- 一些实用的小模板
- Linux iptables防火墙实用模板
- 表单 模板(很实用)
- Linux iptables防火墙实用模板
- 一套的实用完整Makefile参考模板
- 简单实用SQL脚本Part:游标模板
- 一套的实用完整Makefile参考模板
- 简单实用的js模板引擎
- maven plugins 教程
- java---线程内数据共享
- 总结:form中使用onSubmit="return false"防止表单自动提交,以及submit和button提交表单的区别
- JSON解析全面综合,实用篇
- Servlet3的问题
- 实用模板
- double free *** set a breakpoint in malloc_error_break to debug
- SDL2.0版本变动后的一些改动
- 序列化
- iOS7 AVAudioRecorder不能录音
- Dynamic Performance Tables not accessible
- linux c编程system与exec等区别简介
- C/C++ 语言中的表达式求值
- 无需重编 修改程序的静态变量初始值