stl template

#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

void test_macro()
{
#if defined(__sgi)
	cout<<"__sgi"<<endl;
#endif
	
	
	// case 2
#ifdef __STL_NO_DRAND48
	cout << "__STL_NO_DRAND48 defined" << endl;
#else
	cout << "__STL_NO_DRAND48 undefined" << endl;
#endif
	
	// case 3
#ifdef __STL_STATIC_TEMPLATE_MEMBER_BUG
	cout << "__STL_STATIC_TEMPLATE_MEMBER_BUG defined" << endl;
#else
	cout << "__STL_STATIC_TEMPLATE_MEMBER_BUG undefined" << endl;
#endif
	// case 5
#ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
	cout << "__STL_CLASS_PARTIAL_SPECIALIZATION defined" << endl;
#else
	cout << "__STL_CLASS_PARTIAL_SPECIALIZATION undefined" << endl;
#endif
	
	// case 5
#ifdef  __STL_FUNCTION_TMPL_PARTIAL_ORDER
	cout << "__STL_FUNCTION_TMPL_PARTIAL_ORDER defined" << endl;
#else
	cout << "__STL_FUNCTION_TMPL_PARTIAL_ORDER undefined" << endl;
#endif

#if defined(__USE_MALLOC)
	cout<<"0"<<endl;
#else
	cout<<"else"<<endl;
#endif
#if 0
	cout<<"0"<<endl;
#else
	cout<<"else"<<endl;
#endif

}

template<class T>
class print
{
	public:
		//static const int a = 3;
		void operator()(const T& elem)
		{
			cout<<elem<<' ';
		}
};
class TestCls
{
public:
	int a;
	~TestCls(){cout<<"~test()";}
};
void test_placenew()
{
	int arr[10];
	cout<<arr<<endl;
	TestCls* pTest = new(arr)TestCls;
	
	cout<<pTest<<endl;
	pTest->a = 3;
	
	cout<<pTest->a<<endl;
	pTest->~TestCls();

}
void test_foreach()
{
	int a[5]={1,2,3,4,5};
	vector<int> vec(a,a+5);
	for_each(vec.begin(),vec.end(),print<int>());

}

class TestTemplate
{
public:
	template<class T>
	void fun(T a)
	{
		cout<<a<<endl;
	}
};

template<class I>
struct my_traits
{
	typedef	typename I::value_type value_type;
};

template<>
struct my_traits<int*>
{
	typedef	int value_type;
};


template<class T>
struct MyIter
{
	typedef T value_type;
	T* ptr;
	MyIter(T* p=0):ptr(p){}
	T& operator*() const {return *ptr;}
};

template<class I>
typename my_traits<I>::value_type
func(I ite)
{
	return *ite;
}

int main()
{
//	TestTemplate t;
//	double a = 5.2;
//	t.fun(a);

	MyIter<int> iter(new int(8));
	int *p = new int(6);
	cout<<func(p)<<endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值