Template (templates) is a new concept introduced in the ansi-c++ standard. If you use a C + + compiler that does not conform to this standard, you probably won't be able to use the template.
functions Template ( function templates)
Templates allow us to generate generic functions that can accept parameters of any data type and return any type of value without the need for functional overloading of all possible data types. This realizes the function of the macro (macro) to some extent. Their pro
content is available, but the parameter type of the function is to be determined (note: the number of parameters is not to be determined ). For example, a (accurately speaking, a class or a group) function has two parameters, and its function is to return the response. Such functions can be implemented using template functions. As follows.
Template T getmax (T arg1, t arg2){Return arg1> arg2 arg1: arg2; // code segment 1}Is this template-based polymo
1. Companion informationWang Yubong, ITCSC 13-1, school Number: 1302011242. Basic functionsThis assignment is arithmetic, is subtraction between the operation, popular simple.3. Program 1) Form layout2) Program operation4. CodeUsing System;Using System.Collections.Generic;Using System.ComponentModel;Using System.Data;Using System.Drawing;Using System.Linq;Using System.Text;Using System.Threading.Tasks;Using System.Windows.Forms;Namespace windowsformsapplication2{public partialClass Form1:form {P
ArticleDirectory
Function Template)
// Reference http://prglab.com/cms/pages/c-tutorial/advanced-concepts/templates.php
Function Template)
Templates allow us to generate common functions that can accept parameters of any data type and return values of any type, function Overloading is not required for all possible data types. To some extent, this achieves the role of a macro. Their prototype can be either of the following two types:
Template Template
The difference between the
a template that returns the larger of two objects, we can write this:
Template
GenericType Getmax (GenericType A, GenericType b) {return (A>B?A:B);}
In the first line of the statement, we have generated a generic data type template called GenericType. So in the function that follows it, GenericType becomes a valid data type that is used to define two parameters A and B and is used as the return value type of the function
(ObjectO1,ObjectO2); classProgram {Static voidMain (string[] args) { //The requirement is that the maximum value of an arbitrary array is implemented by a delegate Object[] Nu = {0,1,2,2,2,3,4 }; ObjectRe =Getmax (Nu, Tomaxok); Console.WriteLine ("max={0}", re); Console.ReadLine (); } Public Static ObjectGetmax (Object[] nums, Tomaxod del) { Objectmax = nums[0]; for(inti =0; I ) {
main () function* Called when [type variable = class name.] Method Name ([parameter list])* Call an unknown function, and a parenthesis will display the conditions that should be enteredIn some cases, the class name can be omitted, under the same classExamplepublic static int Getmax (int n1,int n2){Return n1>n2?n1:n2;}int A=program. Getmax (1,3);1. Constants:Methods for declaring constants:Const constant (
1, two number of the maximum valuefunction Getmax (A, b) {if (a>b) {return A;}else{return b;}}var Max = Getmax (300,600)Console.log (Max)Minimum value of 2, two digitsfunction Getmin (A, b) {if (a>b) {return b;}else{return A;}Return a}var Min = getmin (400,800)Console.log (Min)3, seek n-m between the andfunction Getsum (n,m) {var getsum = 0;for (var i= n; igetsum+= i;}return getsum;}var sum = getsum (1,10);
Foreachnums function, we define a intfunc function pointer, and the Printnum function is a specific function that satisfies the INTFUNC definition. Here we pass the Printnum function as a function pointer to the FOREACHNUMS function in the main function. int 1,5,666,23423,223 }; Foreachnums (nums,sizeof(nums)/sizeof(int), printnum);The results of the final run are as follows:Through the function pointers, we can block the different kinds of specific processing methods, that is, the uncer
To create a unit test in vs2008One, open vs2008, create a class library Vstsdemo.Because I am in the study of the use of vs2008, other versions of VS, the method should be similar, we study it:)Delete the default generated class and create a Logoninfo class, and then we write a very simple way to get the maximum value in this class Getmax (int[] arynum). The code for the method is as follows:Namespace Vstsdemo{public class Logoninfo{public int
Hdu_3308
This question can be done using a line segment tree. The longest lcis in a range is either in the left son, right son, or in the middle.
Single-point update operations are relatively easy to write, because there is no range limit, you only need to update each parent node on the Line Segment tree in sequence.
The query operation has a range limit, but the lcis of the longest range has not changed, that is, it is either in the left son, in the right son, or in the middle. However, d
(): lookForKey(item) elif item.isKey(): print(‘Found the key at ‘,item)Baseline conditions and recursion conditionsdef countdown(i): print(i) if i-1: countdown(i-1) else : returncountdown(5)54321Quick sort divide and conquerdef Sum(arr): if len(arr): return arr[0] + Sum(arr[1:]) else: return 0Sum([i for i in range(1,101)])5050Maximum value found‘‘‘错误的写法,out of rangedef getMax(arr,index=0
argument between these multiple functions is called overloaded functions.
For example, define the following two functions (function: Take the maximum value)
int Getmax (int x, int y, int z)
{
To do;
}
Double Getmax (double x, double y)
{
To do;
}
As you can see from the top two functions, the function names are the same, but the number of arguments and the types of the arguments are different, so the two f
First, using ABS () to find the absolute value
int Getmax (int a, int b)
{
return (A + B + abs (a))/2;
}
Second, calculate positive and negative numbers by bitwise operation
#define SIGNAL_BIT (x) (x >> (sizeof (int) * 8-1) 1)
int Getmax (int a, int b){int hash[2] = {A, b};int index = Signal_bit (A-B);return Hash[index];}
Third, establish the mapping table,
#define SIGNED_BIT (x) (((x) 0x80000000) >> 31)#
Tag:echotest startup libusr Environment Variables inuxshared no The code of the called function in the static library static library under Linux will be copied to the executable file together at compile time!! Executable files do not need static libraries to be present! II. Construction and use of dynamic libraries under Linux 1. Building the dynamic library gcc-fpic-shared -o Lib library name .so Source file List Example: gcc -fPIC-shared-o libmylib.so max.c min.c NBSP;2, dynamic library
method:The name of the object variable. Method name (argument list);The number of arguments, the data type and order must match the parameter list of the called method declaration.The code is as follows: public class demo11{public static void Printinfor () {for (int i = 0; iSystem.out.println (i);}}public static int Getmax (int i, int j) {int max=0;if (i>j) {Max=i;}else {Max=j;}return Max;}public static void Main (String [] args) {System.out.println
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.