C++ Primer Plus Notes 2 (Chapter 4)

1. Declare the array: short months[12]; Access its elements: month[0]~month[11]
2. Initialize the array: (1)int yam[3]={20, 3, 5}; // List initialization The equal sign can be omitted, or {} does not contain data, and the elements are initialized to 0
(2)int xam[3]; xam[0]=20; xam[1]=3; xam[2]=5;
xam[ 3]={20,3,5};//Wrong, not allowed xam=yam; //Wrong, not allowed to assign an array to another array
long ph[]={25, 92, 3.0}; // False, list initialization prohibits narrowing conversion, floating-point double is converted to long integer for narrowing conversion
3. (1)c=sizeof yam; //The result is the number of bytes in the entire array c=12;
(2)c =sizeof yam[0]; //The result is the number of bytes of the element c=4;
(3) sizeof yam/sizeof yam[0] can be used to calculate the number of array elements
4. C++ Standard Template Library (STL) provides Array substitute-vector, C++11 added template class array.
5. When determining the shortest array required to store a string, don't forget to include the null character at the end.
6.'S' is a character constant, "S" represents a string composed of two characters S and \0. "S" actually represents the memory address where the string is located.
7. Array initialization string constant: char name1[15]="C++OwBody";
sizeof (name1)=15; // the length of the entire array
strlen(name1)=9; // the length of the string stored in the array , Only visible characters are counted, null characters and end characters \0 are not counted;
4. Line-oriented input: Generally, when inputting a name variable, you should use the next line input to read multiple words
(1) cin.getline(name, 20) ; //name: array name, 20: the number of characters can be read Take multiple words that do not exceed the number of characters in a line, Enter to generate a newline character, and replace the newline character with an empty character (1) cin.getline(name, 20) ; //name: array name, 20: the number of characters can be read Take multiple words that do not exceed the number of characters in a line, Enter to generate a newline character, and replace the newline character with an empty character
(2) cin.get(name, 20); cin.get(desetr, 20); // will read Go to the end of the line and keep the newline character in the input queue. Call get() twice in a row. When the first character seen in the second call is a newline character, it is considered that it has reached the end of the line and cannot continue to read.
Correct call method: cin. get(name,20).get();
5. For blank lines or other problems, use the following command to restore the input: cin.clear();
6. The string object is declared as a simple variable, not an array. The program can automatically handle the string size. Use string to add header file #include
7. (1)char charr1[20]; char charr2[20]="jagura";
charr1=charr2; //This array assignment statement is illegal
strcpy(charr1,charr2); / / Copy charr2 to charr1
strcat (charr1, charr2); // append charr2 to the end of charr1,
int len=strlen(charr1); //Determine the number of characters in a string strlen() is a function
(2) string str1; string str2="pather"; str1=str2; //The last sentence is legal
string str3; str3=str1+ str2; str2+=str1; //Legal
int len=str1.size();//Determine the number of characters in the string str1 is an object of the string class, and size() is a method of the string class.
6. char charr[20]; cout<<strlen(charr); //The result of the screen output is uncertain, it may be greater than 20, or less than 20. The initialized array is undefined. strlen() counts the number of characters from the first element of the array until a null character is encountered. For initialized arrays, the position of the first null character is random.
string str; cout<<str.size(); //The screen output result is 0. The length of the initialized string object is automatically set to 0 ;
7. The code to read the next line of input into the array: cin.getline(charr1, 20); //This sentence indicates that cin is an object of the istream class, and getline() is a method of the istream class to
read the next line of input into the string object code: getline(cin.str); //This sentence indicates that getline( ) Is not a class method. In the istrea class, there are class methods for handling double, int
// and other basic types, but there is no class method for dealing with string objects
cin>>str; // legal, a member function cin of the istream class is applied >>x
10. Initialization of structure declaration: #include
struct inflatable{ char name[20], float volume, double price};
struct inflatable{ std::string name, float volume, double price}; //The structure can use the string class as a member, but it must specify the access to the namespace
inflatable duck={"Daphe", 0.12, 9.98};
inflatable Bobu; Bobu=duck; //The structure assignment is valid
struct peak{ int number, char car[20]} Ala, bay; // Ala, bay are the structure peak The two structural variables.
Initialize the structure array: inflatable guest [2]={ {"Daphe", 0.12, 9.98}, {"Bobu", 0.34, 6.34} };
11. Union : different data types can be stored, but only at a specific time Store a type.
12. Enumeration: enum spectrum {red, orange, yellow, green, blue, violet, indigo, ultraviolet }; //Symbol constant, the corresponding integer value is 0~7
spectrum band; //band can only be from the enumeration spectrum Select the value among the 8 defined constants.
Set the value of the enumeration: the specified value must be an integer
enum bits {one=1 ,two=2, four=4, eight=8 };
enum bitstep{ first, second=100, third }; //first defaults to 0, the subsequent uninitialized enumeration is 1 larger than the previous one, third =101
enum {zero, null=0, one, nuero=1}; / /You can create multiple enumerations with the same value. The range of
enumeration values: bits myflag; myflag=bits(126); //Legal, the upper limit is the maximum power of 2-1, 2^8-1=127

Guess you like

Origin blog.csdn.net/lvliang2017232003/article/details/85332232