Book Note: C ++ primer 4th (ch1-11)

Source: Internet
Author: User

Chapter 1 Quick Start
1. Endl is a operator that refreshes the buffer zone associated with the device.
Chapter 2 variables and Basic Types

Reserved Words in C ++

 

1. Initialization and assignment are different.
The initialization methods in C ++ include copy initialization and direct initialization.
There is basically no difference between the two built-in types.
However, there is still a big difference in the Custom class type, sometimes you can only use direct initialization.
2. Definition and Declaration
Defines the variable allocation space and declares the type and name of the variable to the program. Definition is also Declaration: when we define a variable, we declare its type and name.
For example:
Extern int I; it is declared but I is not defined here, which tells the program variable I to be defined elsewhere in the program.
Int I; declares and defines I. After this operation, the program allocates memory space for I.
If the Declaration has an initialization type, it can be treated as a definition, even if the Declaration marks extern
For example:
Extern double Pi = 3.14; this is the definition.
Note that the extern definition can only appear outside the function. (The initialization type can be included only when the extern declaration is outside the function)
3. Scope
Global scope, local scope, and statement Scope
4. the const object is the local variable (File Link) of the file by default. This is different in C ++ and C.
The globally defined non-const variables can be accessed throughout the program, but the const variables only exist in the defined files and cannot be accessed by other files.
To implement access in the entire program, you must specify the const variable as extern.
For example:
// File1.cpp
Extern const int buf_size = 100;
// File2.cpp
Extern const int buf_size;

5. Reference is a composite type
The definition must be initialized and cannot be changed after initialization. It cannot be bound to another object.
A const reference is a reference to a const object.

 

Const references can be initialized to different types of objects and initialized to the right value. Non-const references cannot be bound to the right value.


Chapter 2 standard library types-string and vector
1. The standard library name must be fully defined in the header file.
2. default constructor
The default constructor is the constructor called when the initialization is not explicitly provided. It is called in the form of no parameters, so the constructor does not have any parameters or all parameters provide default real parameters.
If the user-defined class does not explicitly define Any constructor, the compiler will automatically "synthesize default constructor" for the class ".
3. Use CIN to read string: CIN> S
(1) read and ignore all leading blank characters (spaces, line breaks, tabulation)
(2) read the character until it encounters a blank character again, and the read ends.
4. Use Getline to read string
Getline reads the entire line of text, but does not contain line breaks. Even if the first character is a line break, the Getline is stopped, and the string object is set to null. Getline (CIN, line );
In addition, Getline does not ignore spaces at the beginning of each line or in other places.
5. iterator
The begin and end functions in the container are mainly used to return the iterator. The iterator returned by begin points to the first element, and the iterator returned by end points to the next element at the end of the container.
Const_iterator can be used for const vector or non-const vetor, but the element value cannot be changed.

After the const iterator is initialized, it can only point to the elements during its initialization and cannot point to other elements.

 



Chapter 2 arrays and pointers
1. pointer and reference

★Similarities:
1. All are addresses;
The Pointer Points to a piece of memory, whose content refers to the address of the memory, and the reference is the alias of a piece of memory.
★Differences:
1. the pointer is an entity, and the reference is only an alias;
2. You do not need to unreference (*) when using a reference. The pointer must be unreferenced;
3. The reference can only be initialized once during definition, and will not be changed afterwards; the pointer will be variable;
5. The reference cannot be blank, and the pointer can be blank;
6. "sizeof reference" gets the size of the variable (object) to which it points, while "sizeof Pointer" gets the size of the pointer itself (the address of the variable or object to which it points;
Typeid (t) = typeid (T &) always true, sizeof (t) = sizeof (T &) always true,
However, when a reference is a member, the occupied space is the same as that of the pointer (the standard rule is not found ).
7. the pointer and reference auto-increment (++) operations have different meanings;
★Contact
1. Reference is implemented using pointers inside the language (how to implement it ?).
2. For general applications, the reference is understood as a pointer and no serious semantic error is made. A reference is a pointer with limited operations (only content operations are allowed ).


2. pointer to const object and const pointer
The const pointer must be initialized during definition.
3. initialize the dynamically allocated Array
When an array is dynamically allocated, if the array element is of the class type, the default constructor of this type is used for initialization, and if it is of the built-in type, no Initialization is performed.
For example, string * PSA = new string [10]; // call the default constructor of string. All elements are initialized as null.
Int * Pia = new int [10]; // Initialization is not performed.
If an array is followed by a pair of parentheses, the compiler can initialize the array.
For example:
Int * Pia = new int [10] (); // Initialization is 0
Note that for dynamically allocated arrays, the elements can only be initialized as default values of the element type, rather than those of array variables, you can use the initialization list to provide different initial values for array elements.
4. Const Dynamic Array
Initialization values must be provided for built-in const dynamic arrays.
For example:
Const int * pci_ OK = new const int [10] (); // brackets must be added here
For a const dynamic array of the class type, the default constructor must be provided for this type, and the default constructor is used for initialization:
Const string * PCs = new const string [10];
However, the value of the const dynamic array cannot be changed, so it is of little use.
5. dynamically allocate an empty array.
Although an array with a length of 0 cannot be defined in C ++, it is clear that it is legal to call new to dynamically create an array with a length of 0:
Char arr [0]; // compilation Error
Char * CP = new char [0]; // valid, but cannot be referenced
6. String and C-style strings
String and C-style strings involve the challenge of C/C ++ efficiency. There are many discussions on the Internet.
However, it is certain that the string is very efficient and safe, so using string is good.
Note: The C-style string has the same data type as the string literal value. You can use the C-style string where the string literal value is used.

Chapter 1 expressions
1. implicit type conversion
It is worth mentioning that the conversion of const objects
When a non-const object is used to initialize a reference to a const object, the system converts a non-const object to a const object. In addition, you can also convert the addresses (or non-const pointers) of non-const objects to pointers to related const types.
For example:
Int I;
Const int CI = 0;
Const Int & J = I;
Const int * P = & ci;
2. Explicit forced conversion
Static_cast
Dynamic_cast
Const_cast: removes the const attribute of the expression.

Reinterpret_cast

Chapter 1 Statements

Exception Handling

 

Chapter 2 Functions
1. Const Parameters
Although the form parameter of a function is const, the compiler regards the function definition as its form parameter and declares it as a common int type. For example:
Void FCN (const int I ){}
Void FCN (int I ){}
The above two functions are the same, mainly to maintain compatibility with C, because in C, there is no difference between functions with const parameters or non-const parameters.
2. Pass the array through reference
If the parameter is an array reference, the compiler will not convert the array argument to a pointer, but pass the array reference itself. In this case, the size of the array becomes part of the form parameter and real parameter type. The compiler checks whether the size of the array real parameter matches the size of the form parameter.
For example, void printvalues (INT (& ARR) [10]) {}
3. pointer-based reference parameters
When a function needs to process an array and the function body does not depend on the length of the array, it should use a pointer parameter. In other cases, it should use a reference parameter.
4. Default real parameters
If a parameter has a default real parameter, all the following parameters must have a default real parameter. By default, real parameters can only be used to replace the tail real parameters that are missing in function calls.
You can specify the default real parameters in the function declaration or definition. However, a file can only specify the default real parameters for one form parameter once.
5. Const member function.
6. constructor initialization list
7. Function Overloading
The main function cannot be overloaded (in C ++)
Void F (INT, INT );
Void F (double, double );
F (2, 2.5); an error occurs during compilation and the function cannot be determined.
Matching for type enhancement or conversion: for small integer types to int type, if one is int type and the other is short type, for any integer real parameter value, int version is better than short version.
Void ff (INT );
Void ff (short );
Ff ('A') calls ff (INT)
Void manip (long );
Void manip (float );
Manip (3.14); Compilation error, unable to judge, because double can be converted to both long and float Standard

The enumerated type can be upgraded to int type or a larger integer.

Chapter 1 Io operations
1. I/O objects cannot be copied or assigned values
For some reason, the standard library type cannot be copied or assigned values.
(1) only the element types that support replication can be stored in vector or other container types. Because stream objects cannot be copied, they cannot be stored in a vector (or in other containers)
(2) The form parameter or return type cannot be the stream type. If you want to pass or return an IO object, you must pass or return a pointer or reference to the object.
2. Standard Io Library
Header file type
Iostream istream, ostream, iostream
Fstream ifstream, ofstream, and fstream support open and close operations
Sstream istringstream, ostringstream, stringstream

3. Refresh the buffer
Cout <"hi" <flush; no data is added
Cout <"hi" <ends; add null
Cout <"hi" <Endl; add a line feed

 

Chapter 4 ordered containers
1. Ordered containers mainly include vector, list, And deque
2. Pointers are also iterators, so you can use built-in arrays to initialize pointer containers.
3. The elements in the container must satisfy (1) support the value assignment operation (2) can be copied. Most types in C ++ meet this requirement. In addition to the reference type, all built-in or composite types can be used as element types.
4. Assignment of ordered containers
C1 = c2
C1.swap (C2) Exchanges content in C1 and C2.

 

5. Self-Growth of vector containers
Why is vector generally recommended? Because the standard library implements the memory allocation policy when implementing vector: It stores elements continuously at the minimum cost, and thus makes up for the convenience of the access elements.
To enable the vector container to implement fast memory allocation, the actual allocated capacity is more than the current space required. The vector container reserves these additional storage areas for storing newly added elements.
It turns out that, compared with the list and deque containers, the growth efficiency of vector is usually very high.
6. Talk about string
String has multiple insert, assign, and erase operations.
Substr (Pos, n) Operation
Append and replace operations
Find, rfind, find_first_of, find_last_of, find_first_not_of, find_last_not_of

 


7. Container adapter: Stack, queue, priority_queue
Stack operation: Empty (), size (), pop (), top (), push (item)
Queue operation: Empty (), size (), pop (), Front (), back (), push (item)
Priority_queue: Empty (), size (), pop (), top (), push (item)

 

Chapter 2 associated containers

 

Map, set, multipmap, and Multiset
1. Map container
You can define a vecotr <int >:: iterator as the key to associate an int type object, but not a list <int >:: iterator as the key to associate an int type object, because vector <int >:: iterator supports the <operation, and list <int >:: iterator does not support the <operation.

2. Differences between map and set containers
The difference between map and set is that map is a set of key-value pairs, and set is only a set of keys. MAP is suitable for situations where you need to know the correspondence between keys and values, such as dictionaries, set is suitable for determining whether certain values exist, for example, determining whether a person's name is in the blacklist.
The difference between set and list is that the elements in the Set container cannot be modified, but the elements in the list container do not have this restriction. Set is applicable to the set that saves the unchanged values, the list container is suitable for saving elements that will change.
Elements associated with the map and set of containers are stored in an ordered manner.


Chapter 2 generic algorithms
1. generic algorithms never perform operations provided by containers
Generic algorithms do not execute container operations, but rely solely on iterator and iterator operations. The algorithm is implemented based on the iterator and its operations, rather than container operations.
2. The accumulate algorithm uses the third real parameter to determine the accumulation type and initial value.

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.