2016-level Computer C + + assistant work (4) array definition and graceful writing code posture __c++

Source: Internet
Author: User
Tags array definition
Array Definition:First of all, to mention the definition of the array, because the students in the definition of the array is easy to open the data, resulting in the problem of Cross-border. An array of runtime is usually the result of a restricted function, and other unknown errors are possible. Some students also like to adopt the way of defining temporary variables, such as
    int n;
    while (cin>>n) {
        int matrix[n+1];
    }
There may be two problems with this approach: 1. Do not pay attention to the scope of the small set.
2. Excessive memory consumption, resulting in insufficient memory. (This occurs when space requirements are high, such as opening a 10^5 array, which involves heap and stack problems, and interested deep digging)
Then one of the methods recommended by the TA is the usual way of playing ACM (but for engineering development, as it may not be appropriate): Mode 1:
#include <cstdio>
using namespace std;
int value[1000];
int matrix[1000];
int name[1000];
int main () {
    
}

We define a global array on the outside of the main function. The advantage of doing this is that you can open a large array. The 10^6 level is also possible. However, it is not recommended to have a single variable such as int n,m. Such variables are defined as global, easily lead to definition confusion, to forget where they have been defined, and then the program appears inexplicable problems. You may notice that when I define the array, I use the name in English to not long, but also like a word (yes), in the Codeblock when the variable name length is greater than or equal to 3 o'clock, by double-clicking the variable, you can highlight all the position of the variable appears, convenient debug. At the same time define a meaningful variable, you can let yourself understand its meaning, in the confusion of ideas, you can quickly clarify what to do, what to do. The following figure is the highlight.

The more thoughtful students, you can note that I used three of the same length of the array. Leads to a second definition, which takes you to write more graceful code.
using namespace std;
#define MAXN 1000
int VALUE[MAXN];
int MATRIX[MAXN];
int NAME[MAXN];

int main () {
    
}
We use the macro definition #define MAXN 1000 This sentence can be simply understood that later MAXN, will be compiled with 1000 replacement. (Interest deep digging) the effect of writing this is equivalent to the previous code. The advantage is that I just need to modify the value of the MAXN to change the length of the three arrays, and in the case of finding the data range wrong, just change one place. However, the length of each array is different, it is best not to use multiple macro definitions, you can choose the largest one, and then the other arrays are larger. (This is called trick)
Here is a common method of macro definition in ACM
#define LL long
ll a = 0;

Here we'll replace long long, and we can use LL instead of a long integer to make it easier.

Graceful Code formatYes, the purpose of this blog, compared to the second one, is to show you some obvious ways to make yourself B, which we call ' posture ', and a graceful code posture can give you an incredible advantage (and you'll realize it when you write more). Elegant posture code can bring benefits, first, as a teaching assistant can be clearer understanding of your logical structure, fast positioning errors. Second, show the maturity and excellence of your code, and differentiate between novice and professional. Talk is cheap, the code.


#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std;
#define MAXN 1000    //macro definition, can clearly determine whether the range of data to consider the
int VALUE[MAXN];     Good variable definition, can understand the function of this variable, also can quickly locate the position of this variable to appear
int MATRIX[MAXN];
int NAME[MAXN];         Code aligns the same level of code indentation, for
    (int i = 0;i < n; i++) {  //TA strongly recommend the use of parentheses, this way can make the code
                                //appear shorter, simple and capable itself is a graceful
        if (i = = 0) {             //second layer bracket
            u = i;              Code Alignment
            v = i;
            m = u * v + V;  Here is still the effect of the space
        }
        if (i!= 0) {
                         //Note that the code is compact, please do not add too many useless blank lines
        }} return
    0;
}

in the process of actually writing code, I found that some alumni repeatedly define variables such as
    int k=10;
    for (int i = 0;i < K; i++) {
        int k = 8;
        while (k--) {
            int k = 9;
            if (K > 9) Break
                ;
        }
    
This situation can lead to unpredictable errors. Please note the scope of each variable. Do not repeat the definition.


Examples of simple demonstrations like this, can do so, and the good programmers are getting closer. I don't show code that's not very standard, because you just need to see and learn something Good (^_^).








Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.