Simplicity is both beauty, and programmers should try to write concise expressions, to use simple code to achieve more functionality, of course, this depends on the situation (sometimes you have to consider the program run time).
When reading the C++prime Plus to the while statement, there is an example of copying the contents of an array to another array:
ARR1 is an array if ints
int *source=arr1;
size_t sz=sizeof (arr1)/sizeof (*ARR1); Number of elements
int *dest=new Int[sz];
while (SOURCE!=ARR1+SZ)
*dest++=*source++; copy element and increment pointers
First initialize the source and dest, and make them point to the first element of the associated array. The while loop condition determines whether the end of the array to copy is reached. If not, continue with the loop. The loop body has only a single statement, implements the copy of the element, and makes a self-increment operation on the two pointers so that they point to the next element of the corresponding array.
Here, the statement in the loop body:
*dest++=*source++;
That is, the simplicity. This expression is also equivalent to:
{
*dest=*source;
++dest;
++source;
}
Based on the above, I have written the following program on the compiler:
#include <iostream>
using namespace Std;
int main () {
Char arr1[]= "string";
char* source=arr1;
size_t sz=sizeof (arr1)/sizeof (*ARR1);
Char *dest=new Char[sz];
while (SOURCE!=ARR1+SZ)
*dest++=*source++;
cout<<arr1<<endl;
cout<<dest<<endl;
cout<< "**********" <<endl;
cout<<sz<<endl;
delete []dest;
return 0;
}
The function of the program is to copy the array arr1 into dest and output arr1 and dest. But the result of the operation is not what I thought:
problem
First of all, I thought about whether the contents of the array arr1 were not copied into the dest, so I followed the procedure, and finally found the reason:
At first, we define a arr1 array, then dynamically create an array using the new expression and return a pointer to the newly created object dest, and then copy the element with the source pointer and the dest pointer, which does not seem to be wrong. But I overlooked the point that dest is a pointer to a dynamic array instead of an array name, but I took the dest pointer as the array name (cout<<dest<<endl;) at the end of the output.
Let's take a look at how the new expression is described on the C + + Prime Plus: When anew expression dynamically creates an object, it only needs to specify its data type, instead of naming the object, and instead,thenew The expression returns a pointer to the newly created object through which we access the object.
Although I did use the Dest pointer to access the dynamic array we created at the end of the output, we need to know that the dest pointer is not a dynamic array at this point.
, the source and dest Pointers point to the first element of arr1 and the dynamic array, respectively, before the program executes the while loop, but two pointers are executed after the while loop, so it is not right to use the Dest pointer to access the dynamic array at this point.
Workaround: as the book says, for dynamically created objects of the new expression, we access this object through the returned pointer, whereas in this case the Dest pointer acts as a working pointer, so that the dest pointer does not point to the dynamic array at last, which makes it inconvenient for us to access the dynamic array, So we can create another pointer instead of the DEST pointer to act as a working pointer.
The program also gives the following:
...
Char *dest=new Char[sz];
Char *dest_p=dest;
while (SOURCE!=ARR1+SZ)
*dest_p++=*source++;
....
The results of the program run as follows:
Summary:
1 , brevity is both beauty, as a programmer should try to do their own writing programs dapper, with less code than the use of larger functions, experienced programmers attach great importance to brevity. To continue to study similar *dest++=*source++; the code, and finally reached a point of glance.
2 , C + + , in the use of New When an expression creates a dynamic object, we use the returned pointer to access the object, so a new pointer should be created to act as a working pointer, to avoid the problem of accessing this object when the original pointer finally knows where we are not clear.
Simplicity is beauty-Copy an array with a while statement