1. 程式人生 > >STL原始碼剖析:第一章

STL原始碼剖析:第一章

例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);
}


例2:
#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;//由大到小進行排序
}