#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;
}
stl template
最新推荐文章于 2021-12-26 15:18:28 发布