The previous section describes some implementation methods of the function, but does not explain the syntax of the function, that is, what is the use of the function and why it is used. For this, this article and the future will be a few scattered, in the "C ++ from the beginning (12)" more detailed description. This article is just about the basic requirements of programmers-getting algorithms and getting code-some examples are provided to illustrate how to compile C ++ code from algorithms, it also describes multiple basic and important programming concepts (that is, concepts that exist independently of programming languages ).
Code obtained by Algorithms
At the beginning of the series, this series describes what programs are and describes that human programs (algorithms) cannot be compiled due to incompatibility between the world of CPU and the objective physical world) translation into CPU commands, but in order to be able to translate, it is necessary to make people think that something in the CPU world is something that is described by a human-thought algorithm. For example, the pictures displayed on the computer screen show different colors for different pixels through the display, and people think that is an image, while the computer only knows that it is a series of numbers, each number represents the color of a pixel.
In order to implement the above "let people think it is", the first step after obtaining the algorithm is to find out what the algorithm needs to operate on.Resources. As mentioned above, any program describes how to operate resources, while C ++ can only operate on memory values, therefore, the first step for programming is to map the operations in the algorithm into memory values. Because the value of the memory unit and the continuity of the memory unit address can be expressed by the binary number, the first step is to express the operations in the algorithm in numbers.
The first step above is equivalent to mathematical modeling-expressing the problem in a mathematical language, but here it is just to express the operated resources with numbers (pay attention to the difference between numbers and numbers, A number is an operator in C ++ and has a relevant type. It is represented by a number rather than a binary number because it is calculated in binary, to enhance the semantics ). The second step is to map all operations on resources in the algorithm to idioms or functions.
When using mathematical language to express an algorithm, for example, ing the number of people who arrive at the station every 10 minutes to a random variable is the first step above. Then the random variable is subject to the Poisson distribution, that is, the second step above. The number of people waiting for a bus to arrive is the resource to be operated, and the algorithm provided is to change the resource every 10 minutes, change the value to a random value distributed by the Poisson function of the given parameter.
In C ++, the resource has been mapped to a number, and then the operation on the resource is mapped to a number. Only operators can operate on numbers in C ++, that is, all operations on resources in the algorithm are mapped into expression statements.
When the preceding steps are completed, only the execution sequence is left in the algorithm, and the execution sequence is written from top down in C ++, when the execution sequence needs to be changed through logical judgment, the preceding if and goto statements are used (however, the latter can also be implemented through the statement followed by if, this can reduce the use of goto statements, because the semantics of goto is jump rather than "so"), and you can consider whether loop statements can be used to simplify code. The third step is to display the execution process in a statement.
The reason for the previous step 2 is that it can be mapped to a function, that is, a certain operation may be complicated, but it also has a logic meaning that the corresponding operators cannot be directly found, in this case, we had to use the omnipotent function operator to repeat the preceding three steps for this operation to map the operation into multiple statements (such as if statements to display the logical information ), define these statements as a function for the function operator to represent that operation.
It doesn't matter if it is not clear above. The two examples below will explain how the above steps are implemented respectively.
Sort
Three cards are provided, and three integers are randomly written on them. There are three boxes marked as 1, 2, and 3 respectively. Three cards are randomly placed in the boxes 1, 2, and 3. Now we need to sort them so that the integers in the boxes 1, 2, and 3 are in the ascending order.
The simplest algorithm is given: the integers on the cards in the boxes 1, 2, and 3 are the numbers 1, 2, and 3 respectively. Then, the first and second numbers are compared, if the former is large, the cards in the two boxes are exchanged; then the first and third are compared. If the former is large, the cards are exchanged, so that the first number is the smallest. Then compare the second number with the third number. If the former is large, it is switched and the sorting is completed.
Step 1: The resources operated by the algorithm are cards in the box. to map the cards into numbers, pay attention to the differences between the cards in the algorithm and those before. In the algorithm, the only way to distinguish different cards is the integer written on the card. Therefore, a long number is used to represent a card.
The algorithm contains three cards, so they are represented by three numbers. As mentioned above, numbers are in the memory, not in variables, but in ing addresses. Three long-type numbers are required here. The compiler can use the memory allocated automatically on the stack to record these numbers when defining variables, so we can define three variables long a1, a2, a3; to record three numbers, it is equivalent to three boxes containing three cards.
Step 2: The operation in the algorithm is to compare and exchange the integers on the card. The former is simple and can be implemented using logical operators (because the integers on the card are mapped to the numbers recorded in the variables a1, a2, and a3 ). The latter exchange cards in two boxes. You can take a card out of the box and place it on the table or somewhere else. Then, extract the cards from the other box and place them in the empty box. Finally, put the first card into the empty box. As mentioned above, "on the table or somewhere else" is used to store the cards that are taken out. In C ++, only the memory can store numbers, therefore, a temporary memory must be allocated to temporarily record the number.
Step 3: the operations and resources have been mapped. if there is an algorithm, replace it with if, and replace it with, there is something repeated until we replace it with "while" or "do while", as shown in the preceding figure. The algorithm ing is complete, as shown below:
Void main ()
{
Long a1 = 34, a2 = 23, a3 = 12;
If (a1 & gt; a2)
{
Long temp = a1;
A1 = a2;
A2 = temp;
}
If (a1 & gt; a3)
{
Long temp = a1;
A1 = a3;
A3 = temp;
}
If (a2 & gt; a3)
{
Long temp = a2;
A2 = a3;
A3 = temp;
}
}
The preceding composite statement after each if defines a temporary variable temp to provide temporary memory for storing cards with the compiler's static memory allocation function. The element exchange above is not mapped to a function as described above, because it only has three statements and is easy to understand. If you want to define an exchange operation as a function, you should:
Void Swap (long * p1, long * p2) void Swap (long & r1, long & r2)
{{
Long temp = * p1; long temp = r1;
* P1 = * p2; r1 = r2;
* P2 = temp; r2 = temp;
}}
Void main ()
{{
Long a1 = 34, a2 = 23, a3 = 12; long a1 = 34, a2 = 23, a3 = 12;
If (a1 & gt; a2) if (a1 & gt; a2)
Swap (& a1, & a2); Swap (a1, a2 );
If (a1 & gt; a3) if (a1 & gt; a3)
Swap (& a1, & a3); Swap (a1, a3 );
If (a2 & gt; a3) if (a2 & gt; a3)
Swap (& a2, & a3); Swap (a2, a3 );
}}
First look at the program on the left. The function is defined above to represent the exchange operation between given boxes. Note that the parameter type uses long *. Here the pointer indicates reference (note that the pointer can not only represent reference, but also has other semantics, ).
What is reference? Note that this does not refer to the reference variable proposed by C ++. The reference indicates a connection relationship. For example, if you have a mobile phoneMobile phone numberThe Code is a reference of "Talking To You", that is, as long as you have a mobile phone number, you can achieve "Talking To You ".
Another example is the shortcut provided by the Windows operating system, which is a reference to "perform operations on a file". It can point to a file, you can double-click the shortcut icon to "execute" the file it refers to (it may be opened by a software or directly executed ), however, if you delete this shortcut, it does not delete the file it points to, because it is only a reference to "perform operations on a file.
The name of a person is a reference to "identify someone", that is, when someone is admitted to college and says the name of that person, then everyone can know who the person is. Similarly, a variable is also a reference. It is a reference of a block of memory because it maps the address, and the memory block can be uniquely identified by the address, not just the identifier. Note that it is different from the previous name, because any operation on the memory block, as long as you know the first address of the memory block, you have to talk to someone face to face or eat, it is not enough to know his name.
Note that there can be more than one reference to something. For example, a person can have multiple names, variables can also have reference variables, and mobile phone numbers can also be more than one.
Note that the function is introduced to indicate exchange, and the box becomes a resource. Therefore, the box must be mapped to a number. The cards in the box are mapped to numbers of the long type, therefore, we can think of using a memory block that identifies a number that represents the card as the number type mapped to the box, that is, the first address of the memory block, that is, the long * type (note that it is not the address type, because the address type number does not return the address that records its memory ). Therefore, the above function parameter type is long *.
The program on the right is shown below. The parameter type changes to long &. Like the pointer, it still indicates reference, but note their differences. The latter indicates that it is an alias, that is, it is a ing, And the ing address is the address of the number recorded as the parameter, that is, when it needs to call this function, the number given as a parameter must be an address number. The so-called "number with addresses" indicates that this number was created by a programmer, not the address of the temporary memory generated by the compiler for temporary reasons, such as Swap (a1 ++, a2 ); an error is reported. As I have already stated, because the address returned by a1 ++ is set internally by the compiler, it does not exist in the program logic, and Swap (++ a1, a2) is correct. Swap (1 + 3, 34); still reports an error because the memory for recording the numbers returned by 1 + 3 is allocated internally by the compiler. In terms of program logic, they are not recorded by programmers using a certain block of memory, so there will be no memory.
A simple criterion is the parameter type that is called. If the parameter type is an address-type number, yes; otherwise, no.
It should also be noted that the above is a long & type, indicating that the modified variables do not allocate memory, that is, the compiler should statically set the address mapped to the parameters r1 and r2. For Swap (a1, a2); it is the address of a1 and a2 respectively, but for Swap (a2, a3); it is the address of a2 and a3, in this way, the address mapped to r1 and r2 cannot be set at one time, that is, the address mapped to r1 and r2 changes during the program running, and cannot be determined statically at compilation.
To meet the above requirements, the compiler will actually allocate memory on the stack, and then