On 27/12/2017 15:50, 唐彬 wrote:
> where can i find the code or tutorial to learn from, which can better my coding style and let me learn more about how they work?
> thx a lot
You need to provide specific problems for anybody to help you here. You
started with STL but the body is about general coding styles and how to
"learn more".
To give an example, the area of a triangle can be computed as follows:
<!================== Function 01 ==================!>
int AreaofTriangle (int base, int height)
{
return base * height * 1/2;
}
This will work if the dimensions are all integers. What about when
dimensions are in decimals? So you might be tempted to write another
function like so:
<!================== Function 02 ==================!>
double AreaofTriangle2 (double base, double height)
{
return base * height * 1/2;
}
So this will work with numbers like 3.5, 4.5
However, some nit-pickers will come out with some other measurements.
So you could apply a template that can work with most sizes as long as
the logic remains the same as to how to calculate the Area of a
Triangle. So you could use this template:
<!================== Function 03 ==================!>
template <typename T>
T AreaofTriangle3 (T base, T height)
{
return base * height * 1/2;
}