Data.Structures.For.Game.Programmers.PART1.Concepts.1&2

【1.Basic Algorithm Analysis】

Big-O Notation: roughly estimates how the algo scales when it's used on diff sized datasets.
eg: O(function);
  The func is a math formula based on n and c: n represents the num of data elem in the algo and c represents a constant num.
  O(log2 n) O(n) O(n log2 n) O(n2) O(n3) O(2^n)

【2.Templates】

1.A template is a mold for an algorithm or a class, and the programmers decide what type of material they want to use with it.
2.C++ supports 2 kinds of templates: template functions and template classes.
3.The syntax for a template function is such:

template <class T>
returntype functionname( parameter list )


U first declare that you are creating a template by putting in the template keyword.
U then put the class keyword and the name of the generic datatype after that, contained within the <> brackets.T is the name of the generic datatype, and whenever I want to use the class in the function, I refer to it as T.


eg:
template <class T>
T sum(T* p_array, int p_count)
{
	int index;
	T sum = 0;
	for (index = 0; index < p_count; index++)	
		sum += pArray[index];
	
	return sum;
}


int main()
{
	float array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	cout << sum(array, 10) << endl;
	return 0;
}




4.A template class is similar to a template function, except that a template class is an entire class that operates on a generic datatype.


eg:
template < class T >
class Adder
{
public:
	Adder()
	{
		m_sum = 0;
	}
	void Add( T p_number )
	{
		m_sum += p_number;
	}
	T Sum()
	{
		return m_sum;
	}
private:
	T m_sum;
};


This is the syntax required to declare an instance of the adder class that operates on integers:
Adder<int> intadder;


5.Multiple Parameterized Types
  A template can have any number of parameterized types:
template < class one, class two, class three >


eg:
template < class Sumtype, class Averagetype >
Averagetype Average( Sumtype* p_array, Averagetype p_count )
{
	int index;
	Sumtype sum = 0;
	for ( index = 0; index < p_count; index++ )
		sum += p_array[index];
	return (Averagetype)sum / p_count;
}  


NOTE:
The compiler determines the types of a template function implicitly.


6.Using Values as Template Parameters.
template <datatype value>


eg:
template< class Datatype, int size >
class Array...


Array<int, 5> iarray5;
Array<float, 15> farray15;


template <class T, T value>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值