Alas, I really do not want to vomit the hard journey of driving a test. Run to the Great Western suburbs, the Big sun, a day can touch the last 20 minutes of the car, do not too pit dad, these two days really do my butt hurts.
。。
I'm not going to be decisive today. Just pity my big Argentina, persist until the last fall short to defeat in Germany, in vain I four point naturally woke up to see the next game.
Cannot but admire Neuer, RAM. Boateng's defensive line made Argentina a whole game without a few threatening shots. Congratulations to my big Klose in my career in the old age can bring the Spirit cup into my arms ...
Bye Brazil World Cup, bye Argentina, bye, Lionel Messi.
2.1 String Constants
The task of initializing a character array to a string looks tedious--using a large number of single arguments, and you must remember to add null characters.
Don't worry, there's a better one. The method of initializing an array to a string---just need to use a string enclosed in an argument. Such a string is called a string constant, such as:
<span style= "FONT-SIZE:14PX;" >char dog[8]={' B ', ' e ', ' a ', ' u ', ' x ', ' ', ' I '};//not a stringchar cat[8]={' f ', ' a ', ' t ', ' e ', ' s ', ' s ', ' a ', '};//a ' Stringchar bird[11]= "Mr.cheeps"; char fish[]= "Bubbles";</span>
Sometimes the string is very long and cannot be placed on one line. C + + agrees to merge the string constants enclosed in two arguments into one. In fact, no matter what two blank (spaces, tabs and newline characters) delimited string constants will be their own active stitching into one. Therefore, there can be statements such as the following:
<span style= "FONT-SIZE:14PX;" >cout<< "I ' d give my right arm to being" "a great violinist.\n" cout<< "I ' d give my right arm to be a great Violi nist.\n ";cout<<" I ' d give my right arm to "</span>
<span style= "FONT-SIZE:14PX;" > "Be a great violinist.\n";</span>
2.2 The reading of the string into cin and the discussion of Getline
- First of all, how is Cin finished typing the string? Because it is not possible to enter NULL characters through the keyboard, CIN needs to use a different method to know where the string ends.
CIN uses (spaces, tabs, and line breaks) to determine where the string ends. This means that CIN can only read a single word when reading a string. After reading the word, CIN places the string in the array and voluntarily adds the null character at the end.
- Obviously it's not always a good choice to read one word at a time, so we need to use a line-oriented approach instead of a word-oriented method.
At this time The IStream class provides some line-oriented member functions: Getline () and get (). Both of these functions read one line of input. Know the arrival line break. But the difference is that getline () discards the newline character. The Get () preserves line breaks in the input queue.
- The Getline () function is described in the following first. It reads the positive line and enters the end of the line by entering a newline character through the ENTER key.
To invoke such a method can be used Cin.get (). The function has two parameters, the first parameter stores the array name of the row, and the second parameter is the number of characters to read.
Like what
- Cin.getline (name,20);
- Next we'll see another way. The IStream class has a member function called get (). There are several variants of the function. One of the ways of working is similar to getline (), where they accept the same number of references, but the difference is that it's not dropping the newline character, but leaving it in the input queue
<span style= "FONT-SIZE:14PX;" >cin.get (name,artsize); Cin.get (dessert,artsize);//At this time dessert will simply read a newline character and appear empty </span>
- To solve the problem, the Get () function provides a VARIANT that can be used to resolve line breaks to prepare for reading the next line
<span style= "FONT-SIZE:14PX;" >cin.get (name,artsize); Cin.get (); Cin.get (dessert,artsize);//ok</span>
- The last way to use the function is to stitch it together.
The reason why you can do this is cin.get (...) Returns a Cin object that is then used to invoke the CIN function.
<span style= "FONT-SIZE:14PX;" >cin.get (name,artsize). get (); Cin.getline (name1,artsize). Getline (name2,artsize);</span>
Here is a comparison of the easy wrong example, give a little reminder.
<span style= "FONT-SIZE:14PX;" > #include <iostream>int main () { using namespace std;cout<< "What year is Your house built?\n"; int year;cin>>year;cout<< "What's its street address?\n"; Char address (page) cin.getline (address,80);// This will read enter cout<< "Year built:" <<year<<endl;cout<< "done!\n"; return 0;} </span>
2.3 Further talk about Getline
The following is a line of input to the read code:
<span style= "FONT-SIZE:14PX;" >cin.getline (charr,20);</span>
This period notation indicates that the function getline () is a class method of the IStream class. Here's another line of code:
<span style= "FONT-SIZE:14PX;" >getline (CIN.STR);</span>
The period notation is not used here, which indicates that such a method is not a class method. It takes CIN as a reference, where it spends its money to find the input, and secondly he doesn't indicate the length of the string. So why is a getline function a IStream class method? And there's another one, isn't it? Very long before the string class was introduced. C + + has a IStream class. Thus the IStream class takes into account other basic types at these times and does not take into account a type like string. Therefore, there are class methods in the IStream class that deal with double int and other types, and not the class method that handles string.
2.4 Enum types
The enum tool for C + + provides a way to create symbolic constants that can be substituted for Const. It also agreed to define new types, but must be subject to strict restrictions. Take a look at the following statement:
<span style= "FONT-SIZE:14PX;" >enum spectrum{red,orand,yellow,green,blue,violet,indigo};</span>
The statement is complete: let spectrum become a new type. It becomes an enumeration type, followed by Red,orange,yellow as a symbolic constant, with corresponding integer values 0~7. These constants are called enumerators. By default, an integer value is given an enumerator, and the first enumerator has a value of 0. The second one is 1. In turn.
Enumeration variables have the following properties.
<span style= "FONT-SIZE:14PX;" >spectrum band;band=blue;//validband=2000;//invalid,2000 not a enumerator//for enumerations, only the assignment operator is defined, in detail there is no arithmetic operation defined for it: Band The =ORANGE;//VALID++BAND;//ERRORBAND=ORANGE+RED;//ERROR//enumeration type can be promoted to reshape. However, the int type cannot voluntarily convert to an enum-type int color=blue;//valid,spectrum type promoted to intband=3;//errorcolor=3+red;//red converted to Int</span>
As you can see. The rules for enumerations are fairly strict.
In fact, enumerations are more often used to define related symbolic constants, rather than new types.
Suppose you intend to use constants only. Instead of creating a variable of the enumeration type, you can omit the name of the enumeration type. Such as:
<span style= "FONT-SIZE:14PX;" >enum{red,orange,yellow...}; </span>
2.5 issues that you may encounter when you use new and delete
We know that when memory is needed. We can use new to request that when memory is exhausted we use the delete operator to return it to the memory pool. When using new and delete, we should follow these rules:
- Do not use Delete to release memory that is not new allocated
- Do not use Delete to release the same piece of memory
- Suppose you use new "" to allocate memory for an array. You should use delete "" to release
- Assuming that you use new to allocate memory for an entity, you should use Delete (without parentheses) to release
- Applying delete to a null pointer is safe
The following is a brief description of memory leaks or memory exhaustion: The computer may not be able to meet the new request because it does not have enough memory. In such a case. New generally throws an exception. The exception will be explained later.
In C + +. A pointer with a value of zero is called a null pointer. C + + ensures that null pointers do not point to valid data. Therefore, it is used to indicate that the operator or function failed. The leaked memory will not be available for the entire life of the program, and the memory is allocated but not available. Extreme cases are. Memory leaks are so severe that the memory used by the application is consumed and memory-exhausted errors occur, causing the program to crash
2.6 Self-active storage, static storage and dynamic storage
- Self-actively stored in the function of the first general variables using their own active storage is called their own active variables, which means that they are in the function when they are called to generate themselves, at the end of the function to die.
In fact, the active variable is a local variable whose scope includes his block of code.
A code block is a piece of code that is included in curly braces. Self-active variables are often stored in the stack. This means that when you run a block of code, the variables are added to the stack sequentially, and when you leave the code block, the variables are released in reverse order.
- static storage of static storage is the storage method that exists in the whole program running interval.
There are two ways to make a variable a static variable: one that defines it outside the function, and one that uses the Keywordstatic
<span style= "FONT-SIZE:14PX;" >static Double fee=56.50;</span>
- Dynamic storage The new and delete operators provide a more flexible approach than their own active and static variables.
They manage a pool of memory, which is called a free storage space or heap in C + +. The memory pool is common to static variables and the memory of its own active variable is separate. Therefore, the life cycle of the data is completely unaffected by program or function lifetime. Compared to regular variables, using them gives the program ape more control over how the program uses memory.
Some gains from C + + Primer Plus--(ii)