Study Notes: C ++ Primer (4th) and exercises (ch01-ch11) [++], primerch01-ch11
Study Notes: C ++ Primer (4th) and exercises (ch01-ch11) [++]
Chapter 4 data and Basic Types
1. Integer
2. Exercise: Left and Right
3. C ++ keywords/reserved words and operator substitution values
4. Declaration, definition, initialization, and assignment are different concepts.
The declaration indicates the existence of a variable and does not allocate memory space for the variable.
The definition is to describe the existence of a variable and allocate the corresponding memory space for the variable.
Initialization is to fill in meaningful values in the allocated memory space during variable definition. If Initialization is not performed, although the variable has the corresponding memory space, the corresponding content in the memory space is meaningless.
The value assignment is to replace the existing variable value, that is, re-erase the data in the variable memory space and fill in new data.
In C ++, Initialization is further divided into direct initialization and replication initialization because of the existence of classes.
C ++ specifies some rules for variable initialization: the built-in type and class type are different. The built-in type is automatically initialized to 0 in vitro, and the function is not initialized in vivo. The class type calls the default constructor of the class.
5. const variable/object initialization
6. After the declaration is referenced, it must be initialized and cannot be changed once initialized.
You cannot use the const variable to initialize non-const references.
7. defining variables is different from defining class member variables.
Chapter 1 Standard library type
1. Read and Write string objects
The following code continuously reads words from input.
Int main (){
String word;
While (cin> word)
Cout <word <endl;
}
The general output of the above program is as follows:
If it is read from the standard input, the result is as follows: the output is generated every time you press the Enter key. The primary cin reads the input from the buffer zone. The buffer zone is refreshed only when the Enter key is pressed each time.
If the pipeline is used for input, the result is as follows:
We can see that words are divided by blank characters (spaces, carriage return, and tabs. The end is determined based on the file Terminator.
Use getline as follows:
Int main (){
String line;
While (getline (cin, line ))
Cout <line <endl;
}
8. bitset type
Chapter 2 arrays and pointers
1. pointer and reference
2. Dynamic Array Initialization
Chapter 1 expressions
1. In C ++, the prefix or Postfix Operators
2. Default initialization when an object is dynamically created.
3. delete the const object and use delete to reclaim the memory.
4. type conversion in C ++
Chapter 1 Statements
1. Standard Library exception
Chapter 2 Functions
1. participate in real parameters: the form parameter is a variable, and the real parameter is an expression.
2. const Parameters
For C ++ built-in types, because the value replication method is used, adding const or not is not affected.
If the parameters are referenced, the use of const and non-const is different.
3. const member functions
4. Constructor
Default constructor: List of constructor Initialization
5. Real parameter type conversion
C ++ supports function overloading, so there is a function matching problem. In the process of function matching, if the best match is not found, you need to convert the real parameter type to find the sub-optimal match.
6. const parameters and overloading
7. Exercise, enumeration type objects can only be initialized with another object of the same Enumeration type or one enumeration member.
8. const references the form parameter
The following functions are incorrect.
Void swap (const int & a, const int & B ){
Int tmp =;
A = B;
B =;
}
Const is used here to prevent modifications to input parameters.
The following functions are also incorrect:
Void swap (const int * a, const int * B ){
Int tmp = *;
* A = * B;
* B = tmp;
}
The function of const is to indicate that the pointer is read-only and cannot be modified.
9. Different writing methods of function pointers in C ++
When a function overload exists, the function pointer must be precisely matched.
Chapter 1 Standard I/O Library
1. inheritance relationship of IO stream class and header file
2. IO objects cannot be copied or assigned values.
3. The biggest feature of the IO stream in C ++ is the stream status. You can determine the stream status.
4. Use of file stream objects
Chapter 4 ordered containers
1. types defined in sequential containers
2. The swap operation saves the cost of deleting elements.
3. Self-Growth of vector containers
5. substr, append, and replace operations in string
6. string-type operation, which is actually very important, you can use this class of find operation to complete a lot of work.
The following are examples of string functions:
View Code
View Code
The program below is not strictly related to this exercise. The program only finds words one by one.
View Code
Chapter 2 associated containers
1. Associated containers in STL
Added the unordered_map, unordered_set, unordered_multimap, and unordered_multiset containers in C ++ 11. These four containers are actually common hash_map, hash_set, hash_multimap, and hash_multiset containers.
2. pair type
3. map container-defined types
The pair type is obtained by referencing the map iterator.
4. Special Features of map subscript access
5. insert operations for map
A simple program for counting the number of words:
View Code
6. search operations in map: count and find Functions
7. A simple word Conversion Program
View Code
The running result is as follows:
8. Differences between map, set, and list
9. definition and use of set
10. Use set to exclude some common words, so that common words are avoided during statistics.
View Code
11. Integrated Application of containers: Text query Program
View Code
If the source file of the program is input, the following output is available:
12. Summary of common Common Containers
Chapter 2 generic algorithms
1. Insert iterator
2. iostream iterator
3. Use the copy algorithm to write files to the output:
View Code
4. Three iterators
5. iterator Classification