I. Array
1. Arrays can only be initialized when they are defined. One array cannot be assigned to another array.
Int a [4] = {1, 2, 3, 4}; // correct
Int a [4];
A [4] = {1, 2, 4}; // incorrect
Int B [4] = a; // incorrect
2. When initializing an array, if some values are provided less than the number of array elements, the remaining values are assigned 0;
Float a [5] = {1, 2 };
3. If [] in square brackets is null during array initialization. The C ++ compiler calculates the number of elements.
Short a [] = {1, 2, 3, 4, 5 };
Int number = sizeof (a)/sizeof (short); // calculate the number of elements (number of visible characters)
Char a [] = {'A', 'B', 'C', 'D', 'E'}; // The string must be of the char type.
Strlen (a); // calculate the number of visible strings
2. String
1. The end character of the string is '\ 0' (ASCII code: 0)
Char a [5] = {'A', 'B', 'C', 'D', 'E'}; // a pure character array, preferably not processed as a string
Char a [5] = {'A', 'B', 'C', 'D', '\ 0'}; // The end is a NULL Character and can be processed as a string.
Char a [15] = "I am a student"; // the system automatically adds '\ 0' to the last character ';
Char a [] = "I am a student"; // The number of characters automatically calculated by the System
[Remember] Do not forget to end an empty string when determining the shortest array required by the string.
"S" indicates two characters: 's' and '\ 0 ';
2. String input: getline () for line-oriented input ()
Char a [20]; // it must be a char-type character array
Cin. getline (a, 20); // read the characters entered by the user into an array of 20 elements.
Cout <strlen (a); // number of output characters
If you enter more than 19 characters (1 '\ 0'), geline only takes the first 19 characters. More than one will not stay in the input queue (difference and get ())
3. String input: Line-oriented input get ()
Char a [2];
Char B [2];
Cin. get (a, 2 );
Cin. get (B, 2 );
Cout <strlen (a) <endl;
Cout <strlen (B) <endl;
Input: abcd
Output: 1 1 If getline (), output: 1 0
Get () takes n-1 visible characters. If the array is declared as n, it reads n-1 characters, and the last one is set to '\ 0'. In case of line breaks, it is left in the input queue.
The second call of cin. get () to read the first character is a line break.
[Difficulty] Why can I enter two cines only once ??
Char a [5];
Cin. get (a, 5 );
Cin. get (); // read and discard line breaks in the input queue. This prevents the next row break from being terminated. The second time cannot be entered.
Cout <a [1] <endl;
Char B [5];
Cin. get (B, 5 );
Cout <B [0] <endl;
Iii. string type
1. connect two strings
String str1, str2, str3;
Str3 = str1 + str2;
/* The above is the C ++ practice, and the following is the C language practice */
Strcpy (str3, str1); // copy str1 to str3
Strcat (str3, str2); // Add str2 to str1
2. The uninitialized character array where the first null character appears is random.
Char a [10];
Cout <strlen ();
Output 4 // 0-9 random
4. Structure Types
1. struct usage
Struct node
{
Int data;
Node * next;
} P1; // declare a variable of the Structure Type
2. union
One data structure format stores different structural types, but only one type can be used at a time.
Struct node
{
Int data;
Union id
{
Long id_num;
Int id_num;
}
Node * next;
} P1 ;//
/* It's time to go to bed ...... Not complete */
Bytes --------------------------------------------------------------------------------------------------------------
/* During the three-day holiday on New Year's Day, I was lazy. Continue fighting */
3. enum Enumeration
Usage: enum enumeration name {elements contained };
Default enumerated values: enum name {red, orange, yellow, green, blue} // default value: red = 0; orange = 1; yellow = 2 ......
Set the enumerated values: enum name {one = 1, two = 2, four = 4, a, B}; // After the default value four is specified, a = four + 1 = 5; B = 6
Declaration variable: name band;
Band = red; // if valid is not forcibly converted, only the enumerated quantity used for enumeration can be assigned to the enumerated variable.
Band ++; // invalid does not define arithmetic operations for enumeration
Band = 2; // If invalid is assigned an integer value to the enumerated type, the type is incorrect.
Int color = 2 + red // valid: int type, 2
Band = band (2); // according to {two = 2} band = two; the integer type can be converted to the enumeration type by force conversion.
4. pointer and free storage space
1) usage: int a = 6;
Int * p; // '* P' indicates the value stored at the p address.
P = & a; // the pointer is p, which stores the value address rather than the value itself.
Or write it as: int * p = & a; // set the pointer p value to & a instead of the * p value.
2) pointer hazard
Long * length;
* Length = 25365; // length is indeed a pointer, but it does not indicate the point, because the pointer length is not initialized.
3) new/delete apply for and release memory
Int * pn = new int; // allocate int-type memory and assign the block address of the memory to the pointer.
* Pn = 100; // assign a value to the declared memory unit
Delete pn; // release the memory unit to which the pn points. The pn pointer itself is not deleted (pn can point to another newly allocated memory block)
[Note] Do not try to release released memory blocks. delete pn; then delete pn will have unexpected errors.
You cannot use delete to release the memory obtained by declaring variables (only new applications can be released) int * p = & a; // You cannot delete
4) new creates a dynamic array
Int * a = new int [100];
Delete [] a; // [] indicates that the program releases an array.
Principle]
Do not use delete to release memory not allocated by new
Do not use delete to release the same memory block twice.
If new [] is used to allocate memory for the array, delete [] should be used to release the array.
If new is used to allocate memory for the variable, delete should be used to release the memory. It is safe to apply delete to null pointers.
5. pointer, array, and pointer Arithmetic
1, double a [3] ={ 100,200,300 };
Double * p = a; // The p Pointer Points to the address of the first element of array.
Cout <"Address:" <p + 1 <"storage value:" <* (p + 1) <endl; // output the address of a [1] and the value of a [1 ].
Note: The increase in p + 1 is equal to the number of bytes in which the p Pointer Points to the type (double 8 bytes)
C ++ interprets the array name as the address of the first element of the array.
2. sizeof indicates the number of bytes occupied by the array and the pointer length.
Program:
[Html]
# Include "stdio. h"
Int main ()
{
Double a [3] = {100.0, 200.0, 300.0 };
Double * p =;
Printf ("display the number of bytes occupied by the array: % d \ n", sizeof (a); // output 24 = 3*8
Printf ("display pointer count: % d \ n", sizeof (p); // output 4 = 3 visible elements with an Terminator
}
3. the array name is the first element address, but the cout object considers the address of the char array as a string address.
Program:
[Html]
# Include "stdio. h"
# Include <iostream>
Using namespace std;
Int main ()
{
Double a [3] = {100.0, 200.0, 300.0 };
Char B [5] = "rose ";
Cout <"print double array name a:" <a <endl; // address of the first element in the output Array
Cout <"Print char array name B:" <B <endl; // output string
}
When cout outputs a char array, the output from the first character element to the null character '\ 0'
4. Do not use constants or Uninitialized pointers to accept input.
Char * p;
Const char * a = "rose ";
Cin> p; // incorrect
Cin> a; // incorrect
5. A program example using new and delete
[Html]
# Include "stdio. h"
# Include <iostream>
Using namespace std;
Char * getname ();
Int main ()
{
Char * name;
Name = getname ();
Cout <name <"at" <(int *) name <endl;
Delete [] name;
Name = getname ();
Cout <name <"at" <(int *) name <endl;
Delete [] name;
}
Char * getname () // if you want to create a large number of arrays, You can dynamically allocate the memory size based on the size of the variable to be stored.
{
Char temp [80];
Cout <"Enter last name :";
Cin> temp;
Char * pn = new char [strlen (temp) + 1];
Strcpy (pn, temp );
Return pn;
}
Program Description: The two output addresses are the same. If delete [] name is removed, the address that is used but not released cannot be used.
From the column of ODA