【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>