C++primerplus 6th Edition, chapter fourth--Composite type

Source: Internet
Author: User

1, composite types mainly include: arrays, structs, unions, enumerations, classes, pointers, references, and so on.

2, Array. Length must be determined. That is, the length of the array must be determined as the compilation phase. You can only declare array lengths using Constants (#define, const). If you use variables to declare the length of an array, the compiler does not know how much memory space to allocate.

3,typename Arrayname[arraysize]. The array subscript starts at 0.

4, array initialization rules:

C + +: The array name is the address of the first element of the array, and you cannot assign an array directly to another array. And the initialization of the array can be initialized with a list, and the assignment can only be assigned by one element of an element. Such as:

All three of the above are available in a way that can be initialized. The first two elements of Hoteltips are 5.0, 2.5, and all elements of the three elements 0;totals are 0;things a four element, respectively, 1,5,3,8. The number of array elements is computed by the compiler.

C++11 rule: List initialization. Narrowing changes are not allowed.

5, String:

C-style strings, string classes.

C-style string: Character data, followed by an empty character, +. The ASCII code for the null character is 0. For example:

Character arrays are not necessarily C-style strings, C-style strings must be character arrays.

The 6,sizeof keyword gets the memory space size of the variable. The header file CString has a strlen function that can get the string length. That is, the number of characters in front of the empty character. Does not include null characters. So if a string m. An array space of at least strlen (m) +1 is required for storage.

7,

, the left side of the source file, the right is the program running results.

Because the keyboard cannot enter a null character, the C + + uses white space characters (spaces, tab keys, newline characters) to indicate the end of the string. We enter Alistair Dreeb. The IO input buffer contains Alistair Dreeb, and when you enter an array of name characters, you end the input by touching a space. IO input buffers are left Dreeb. Next input (Cin>>dessert), the program reads the DREEB directly without requiring the user to enter the data again.

8,cin.getline (). To solve the above problem, call Cin.getline () to read one row of data at a time.

Cin.get (). Similar to the previous method, the only difference is that the former reads a line and discards the newline character. The latter does not discard line breaks.

You can read a line this way and discard the newline character. (using overloaded thinking here):

9,

The left is the source code, the middle is the program output, and finally the solution.

The address has no opportunity to enter, because after the program reads 1966, there is a newline character left in the input buffer. When the address is read, the line break is considered to be a blank line read in. This problem can be resolved in the final way. The empty line is read through Cin.get ().

10,string class:

 

C++primerplus 6th Edition, chapter fourth--Composite type

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.