From this Blog, I will start writing blogs in English. Since this is my first attempt to write English version blogs and I am not a native speaker of English, there might be some issues with grammar, spelling, or the naturalness of expression. If you ever find anything unclear or awkward, I sincerely apologize and I would really appreciate it if you could tell me the problem by email (brad_huang@outlook.com). Thank you for your understanding and support! Let us get start!
Let us take a look of a piece of code:
1 |
|
At first glance, how many problems — that is , instance of nonstandard code — can you find? Well, except for int main() , which is standard, everything else is nonstandard. Let me explain it step by step.
Firstly, the first line #include <bits/stdc++.h> is a universal header. but it is not part of the C++ standard library. Many compilers don’t support it (except gcc). Moreover, it may cause serious naming conflicts and introduce a large number of macros, both of which may lead to unexpected erros — a real disaster for a big project with thousands of lines of code. In addition, this nonstandard header may increase the time spent for compiling. In short, avoid using bits/stdc++.h !
Secondly, using namespace std should not be used here at all, because it may cause undesirable name collision. For example, if you define your own class string and include using namespace std, the compiler will be confused when it encounters string. It doesn’t know whether to use your class or the one from the standard library. Thus, do not use using namespace std;. On the contrary, prefix names with std:: (as shown in the example below).
1 | using std::cout; |
Thirdly, themain function doesn’t need a return statement. Although it is okay for you to write return 0; at the end of main function and I believe many students were taught to include it when they first learned C++, the compiler can automatically insert return 0; at the end of main function. But let me emphasize it again, it is still completely okay for you to write return 0; at end of main function (even I still like to include it myself).