例1:
#include <iostream>
using namespace std;
/*模板一般化设计*/
template<class I, class O>
class TestClass
{
public:
TestClass(){cout << "I, O" << endl;}
};
/*模板特殊化设计*/
template<class T>
class TestClass<T *, T *>
{
public:
TestClass() {cout << "T*, T*" << endl;}
};
/*模板特殊化设计*/
template<class T>
class TestClass<const T *, T *>
{
public:
TestClass() {cout << "const T *, T" << endl;}
};
int main()
{
TestClass<int, char> t1;
TestClass<int *, int *> t2;
TestClass<const int *, int *> t3;
system("pause");
exit(0);
}
#include <iostream>
using namespace std;
class alloc{};
template<class T, class Alloc = alloc>
class vector
{
public:
void swap(vector<T, Alloc> &) {cout << "swqp()" << endl;}
};
template<class T, class Alloc>
void swap(vector<T, Alloc> &x, vector<T, Alloc> &y)
{
cout << "This function is swap(x, y):" ;
x.swap(y);
}
int main()
{
vector<int> x, y;
x.swap(y);
swap(x, y);
system("pause");
exit(0);
}
例3:
#include <iostream>
using namespace std;
class alloc{};
template<class T, class Alloc = alloc>
class vector
{
public:
typedef T value_type;
typedef value_type * iterator;//typedef T * iterator;
template<class I>
void insert(iterator position, I first, I last)
{
cout << "insert()" << endl;
}
};
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
vector<int> x;
vector<int>::iterator ite = NULL;//如果不进行初始化,则编译会出现错误
x.insert(ite, arr, arr+5);
system("pause");
exit(0);
}
例4:
/*测试template能否根据前一个template的参数而设定默认值*/
#include <iostream>
#include <cstddef>
using namespace std;
class alloc
{
};
template<class T, class Alloc = alloc, size_t BufSize = 0>
class deque
{
public:
deque(){cout << "deque" << endl;}
};
template<class T, class Sequence = deque<T> >
class stack
{
public:
stack() {cout << "stack" << endl;}
private:
Sequence c;
};
int main()
{
stack<int> x;
system("pause");
exit(0);
}
例5:
#include <iostream>
#include <cstddef>
using namespace std;
class alloc{};
inline size_t __deque_buf_size(size_t n, size_t sz)
{
return n != 0 ? n : (sz < 512 ? size_t(512 / sz):size_t(1));
}
template<class T, class Ref, class Ptr, size_t BufSize>
class __deque_iterator
{
public:
typedef __deque_iterator<T, T &, T *, BufSize> iterator;
typedef __deque_iterator<T, const T &, const T *, BufSize> const_iterator;
static size_t buffer_size()
{
return __deque_buf_size(BufSize, sizeof(T));
}
};
template<class T, class Alloc = alloc, size_t BufSize = 0>
class deque
{
public:
typedef __deque_iterator<T, T &, T *, BufSize> iterator;
};
int main()
{
cout << deque<int>::iterator::buffer_size() << endl;//BufSize = 0; 返回512/sizeof(int) = 512/4 = 128;
cout << deque<int, alloc, 64>::iterator::buffer_size() << endl;//BufSize = 64; 返回BufSize
system("pause");
exit(0);
}
例6
/*测试前缀++/-- 和后缀++/-- 和指针解引用*/
#include <iostream>
using namespace std;
class INT
{
friend ostream & operator<<(ostream & os, const INT & t);
public:
INT(int i):m_i(i){}
//++INT
INT & operator++()
{
++(this->m_i);
return *this;
}
//INT++
const INT operator++(int)
{
INT temp = *this;
++(*this);
return temp;
}
//--INT
INT & operator--()
{
--(this->m_i);
return *this;
}
//INT--
const INT operator--(int)
{
INT temp = *this;
--(*this);
return temp;
}
//*INT = INT.m_i
int & operator*()const
{
return (int &)m_i;
}
private:
int m_i;
};
ostream & operator<<(ostream & os, const INT & i)
{
cout << "m_i = " << i.m_i << endl;
return os;
}
int main()
{
INT i(5);
cout << i++;
cout << i;
cout << ++i;
cout << i--;
cout << i;
cout << --i;
cout << *i << endl;
system("pause");
exit(0);
}
例7
例8:function call操作符( operator() )
/*将operator()重载的例子*/
#include <iostream>
using namespace std;
template<class T>
class plus
{
public://必须声明在public部分中,如果不声明为public,默认是private
T operator()(const T &x, const T &y){ return x + y; }
};
template<class T>
class minus
{
public://必须声明在public部分中,如果不声明为public,默认是private
T operator()(const T &x, const T &y){ return x - y; }
};
int main()
{
plus<int> plusObj;
minus<int> minusObj;
cout << plusObj(3, 5) << endl;
cout << minusObj(5, 3) << endl;
system("pause");
exit(0);
}
例9:C语言中利用qsort进行排序
#include <iostream>
#include <cstdlib>
const int num = 10;
using namespace std;
int fcmp(const void * elem1, const void * elem2);
int main()
{
int arr[num] = {33, 45, 4, 9, 90, 44, 23, 56, 67, 89};
cout << "原数组的内容:\n";
for (int i = 0; i < num; i++)
cout << arr[i] << " ";
cout << endl;
qsort(arr, num, sizeof(int), fcmp);
cout << "由小到大进行排序之后的数组内容:\n";
for (int i = 0; i < num; i++)
cout << arr[i] << " ";
cout << endl;
system("pause");
exit(0);
}
int fcmp(const void *elem1, const void * elem2)
{
return *(int *)elem1 - *(int *)elem2;//由小到大进行排序
//return *(int *)elem2 - *(int *)elem1;//由大到小进行排序
}