📄 templatezhou.cpp
字号:
#include <stdio.h>
struct NullType{};
template<int i> struct Int{enum{value=i};};
//class的常量是int
template<typename L, typename R>
struct Pair
{
typedef L Left;
typedef R Right;
};
#define IntToPair(x) Pair<Int<x>, NullType>
/* Here are some facilities to manipulate int lists */
/* 1. Get the length of an int list */
template<typename T>
struct Length
{
enum{result = 0};
};
template<typename L, typename R>
struct Length<Pair<L, R> >
{
enum{result = 1+Length<R>::result };
};
/* 2. Display an int list */
template<typename T>
struct Display
{
void show()
{
printf("Unknown");
}
};
template<>
struct Display<NullType> //特例化的标志,类名后有<>
{
void show()
{
printf("Null");
}
};
template<int i>
struct Display<Int<i> >
{
void show()
{
printf("%d", i);
}
};
template<typename L, typename R>
struct Display<Pair<L, R> >
{
void show()
{
printf("( ");
Display<L>().show();
printf(" ");
Display<R>().show();
printf(") ");
}
};
/* 3. Add an int value to the end of an int list */
template<typename T, int i>
struct Append
{
typedef IntToPair(i) Type;
};
template<typename L, typename R, int i>
struct Append<Pair<L, R>, i>
{
typedef Pair<L, typename Append<R, i>::Type> Type;
};
/* 4. Reverse an int list */
template<typename T>
struct Reverse
{
typedef NullType Type;
};
template<int l, typename R>
struct Reverse<Pair<Int<l>, R> >
{
typedef typename Append<typename Reverse<R>::Type, l>::Type Type;
};
/* 5. Concatenate two int lists */
template<typename L, typename R>
struct Concatenate
{
typedef Pair<L, Pair<R, NullType> > Type;
};
template<typename R>
struct Concatenate<NullType, R>
{
typedef Pair<R, NullType> Type;
};
template<typename L, typename R>
struct Concatenate<NullType, Pair<L,R> >
{
typedef Pair<L, R> Type;
};
template<typename L1, typename R1, typename L2, typename R2>
struct Concatenate<Pair<L1, R1>, Pair<L2, R2> >
{
typedef Pair<L1, typename Concatenate<R1, Pair<L2, R2> >::Type> Type;
};
/* 6. Insert an int to a SORTED int list */
template<bool, typename T, typename F>
struct If
{
typedef T Type;
};
template<typename T, typename F>
struct If<false, T, F>
{
typedef F Type;
};
template<typename T, int i>
struct Insert
{
typedef IntToPair(i) Type;
};
template<int l, typename R, int i>
struct Insert<Pair<Int<l>, R>, i >
{
typedef typename If< l<i,
Pair<Int<l>, typename Insert<R, i>::Type>,
Pair<Int<i>, Pair<Int<l>, R> > >::Type Type;
};
/* 7. Sort an int list */
template<typename T>
struct Sort
{
typedef NullType Type;
};
template<int l, typename R>
struct Sort<Pair<Int<l>, R> >
{
typedef typename Insert<typename Sort<R>::Type, l>::Type Type;
};
/* 8. Some macros to simplify the operations */
#define pair(a, b) Pair<a, b>
#define cons(i, T) Pair<Int<i>, T >
#define length(list) Length<list>::result
#define assign(name, type) typedef type name
#define append(list, i) Append<list, i>::Type
#define reverse(list) Reverse<list>::Type
#define concat(a, b) Concatenate<a, b>::Type
#define insert(list, i) Insert<list, i>::Type
#define sort(list) Sort<list>::Type
#define show(list) \
do{ printf(#list ": \n"); \
Display<list >().show(); \
printf("\n"); }while(0)
assign(MyPhoneNum, cons(1, cons(3, cons(9, cons(5, cons(1, cons(0,
cons(7, cons(1,cons(7, cons(8, IntToPair(4))))))))))));
int main()
{
show(MyPhoneNum);
show(cons(9, MyPhoneNum));
show(append(MyPhoneNum, 888));
show(sort(MyPhoneNum));
show(insert(sort(MyPhoneNum), 6));
assign(ReverseNum, reverse(MyPhoneNum));
show(ReverseNum);
show(pair(MyPhoneNum, MyPhoneNum));
show(concat(MyPhoneNum, ReverseNum));
printf("Numbers: %d\n", length(MyPhoneNum));
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -