hi

Published April 04, 2005
Advertisement
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.
Next Entry Untitled
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

I love the morning!

1012 views

Not bad.

1165 views

Zoned out.

1043 views

more

1123 views

Slow down champ.

939 views

I'll be back

898 views

Busy.

1058 views

oh teh noes!!

945 views
Advertisement