Advanced technical scholars compile C ++ code

Source: Internet
Author: User

C ++ code has many mature libraries for network communication, the most representative of which is the cross-platform and heavyweight ACE library, this library can be said to be one of the most important achievements of the C ++ language. We will study it next and hope you can get what you want.

Especially at the beginning of a semester, I often receive a lot of questions about writing a very simple program. There is a representative solution to this problem, that is, reading a few numbers in your program, processing them, and outputting the results. The following is an example of C ++ code:

 
 
  1. # Include<Iostream> 
  2.  
  3. # Include<Vector> 
  4.  
  5. # Include<Algorithm> 
  6.  
  7. Using namespace std;
  8.  
  9.  
  10.  
  11. Int main ()
  12.  
  13. {
  14.  
  15. Vector<Double>V;
  16.  
  17.  
  18.  
  19. Double d;
  20.  
  21. While (cin>>D) v. push_back (d); // read the element
  22.  
  23. If (! Cin. eof () {// check whether the input is incorrect
  24.  
  25. Cerr<<"Format error \ n ";
  26.  
  27. Return 1; // an error is returned.
  28.  
  29. }
  30.  
  31.  
  32.  
  33. Cout<<"Read"<< V. size()<<"Elements \ n ";
  34.  
  35.  
  36.  
  37. Reverse (v. begin (), v. end ());
  38.  
  39. Cout<<"Elements in reverse order: \ n ";
  40.  
  41. For (intI=0; I<V. size(); ++ I) cout<< V[I]<<'\ N ';
  42.  
  43.  
  44.  
  45. Return 0; // return success
  46.  
  47. }


Observation of this program: This is a standard iso c ++ program that uses the standard library ). The standard library tool declares in The namespace std That it is encapsulated in a header file without the. h suffix. If you want to compile it in Windows, you need to compile it into a "console program" console application ). Remember to add the. cpp suffix to the source file. Otherwise, the compiler may think that it is a piece of C ++ code rather than C.

Yes, the main () function returns an int value. Reading a standard vector can avoid overflow errors in a buffer of arbitrary size. Reading an array without generating a "silly error" is beyond the capabilities of a newbie-if you do, you are no longer a newbie. If you doubt this, I suggest you read my article "Learning Standard C ++ as a New Language "), you can download it from my publications list.

! Cin. eof () is a flow format check. In fact, it checks whether the loop ends when an end-of-file is found (if not, it means that the input is not in the given format ). For more information, see the "stream state" section in your C ++ textbook. Vector knows its own size, so I don't need to calculate the number of elements.

  • Describes the problems and skills of the C ++ Language
  • Exploring various concise and flexible features of C ++
  • How to Use the Visual C ++ subset to search for a topic
  • How to better learn C ++ -- and a summary of C ++ learning methods
  • Advanced Programming instructions on C ++ learning methods

This program does not contain explicit memory management. Vector maintains a stack in memory to store its elements. When a vector needs more memory, it will allocate some resources; when it no longer exists, it will release the memory. Therefore, users do not need to worry about the memory allocation and release of elements in the vector.

The program ends when an "end-of-file" is entered. If you run it on UNIX, "end-of-file" is equal to Ctrl + D on the keyboard. If you are on a Windows platform, it cannot identify the "end-of-file" character due to a BUG. You may prefer to use the following slightly more complex version, it uses the word "end" to indicate that the input has ended.

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.