Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

hi

hi

mozie
Not your journal. · · 2 min read
1,147 0
Templates give me this feeling, like you can do anything with them. But I feel like I dont know what to do with them. So I have been just writing generic algorithms, the first few I picked up off of DADS 'M' section, and the rest have been just off the top of my head ones. Nothing really special, and I think I have already overwritten/deleted a few, but I still wanted to share them for anyone who needs some examples. So here is my little bit of template functions.
using namespace std;template <class T>class	Stupid{public:	T	data;	int	cnt;	Stupid()	{		data = T();		cnt = 0;	}};	template <class T>class Trouble{	T	data;public:	Trouble(T d) { data=d; }	T	get() { return data; }};template <>class Trouble <int>{	int	data;public:	Trouble(int d) { data=d; }	Trouble() { data = 1; }	int	get();};int	Trouble<int>::get(){	return data + 12;}template T Mean( FwItr b, FwItr e ){	T sum = T();	T cnt = T();	for(; b != e; ++b, ++cnt) sum += *b;	return cnt ? sum/cnt : T();}template T	Midrange( FwItr b, FwItr e ){	typedef typename std::iterator_traits::value_type value_type;	value_type min(*b), max(*b);	for(; b != e; ++b)	{		if( *b > max )			max = *b;		if( *b < min )			min = *b;	}	return T( min + max ) / 2;}template vector	Mode( FwItr b, FwItr e ){	vector	modeList;	bool	moding = true;	vector >	StupidList ( e - b );	vector >::iterator itera;	cout << (T)StupidList.size() << endl;	for(;b != e;++b)	{		//find(StupidList.begin(), StupidList.begin(), *b);	}	return modeList;}template <class T>vector	RandVect( T min, T max, int num ){	vector store (num);	vector::iterator b = store.begin();	vector::iterator e = store.end();	srand((unsigned)time(NULL));	for(;b!=e;++b)		*b = rand() % (max - min) + min;	return store;}template <class T, typename FwItr>T	Arcane(FwItr b, FwItr e){	T	t = T();	for(;b != e; ++b)	{		t += (T)(0.5 * *b);	}	return t;}

Not much there really, and nothing special (Mode() does not actually preform said function). RandVect is the most recent, and I guess its the one I actually use. I wanted to pump vectors of random numbers through the math functions, so I decided that would be a great use of templates.

Discussion

Loading comments...