Some key notes in c++--Basic learning

Source: Internet
Author: User
Tags shallow copy throw exception

1. Exception Handling:

A) in the try block, once an error occurs, an exception is thrown immediately and then into the catch block (the remaining code in the try block is not executed ).

b) If the throw exception is not captured, it will cause the program core dump ( exception handling does not have a corresponding code block, which will also cause core dump).

c) for different types of exceptions, different catch blocks can be used to deal with them, but also can take a unified processing ( the more common exception handling handle, the more should be put to the end ).

d) exception in a function does not have the corresponding processing, will continue to throw out (the function remaining code will not be executed, the program will immediately exit the program ).

2. Pass the QUOTE:

A) The value passed in the C language is actually a copy of the value of the argument to the formal parameter when the function is called ( if the type being passed is more complex, it can cause a significant overhead ).

b) in C + +, we use a pass-through reference to avoid the overhead of object replication ( both the original object can be modified, and the cost of replication can be avoided ).

c) If the formal parameter of a function is a &a, this means that the function can not only change the value of a, but also expects to change the value of a .

d) Aconst reference represents a constant light, which is a protection semantics, not a const reference, which is a modification semantics .

e) Never return a reference or pointer to a local object .

3. Macro functions and inline functions:

A) macro function:

The text is expanded during preprocessing.

There is no overhead for function calls.

The disadvantage is that the compiler cannot check for errors if it is not called (the compiler cannot find this code at all, because it disappears during the preprocessing of the Code ).

b) Inline function:

Can be considered an advanced macro function.

is expanded inline during compilation.

There is no overhead for function calls.

Inline functions require the compiler to check for syntax errors.

4. Unique labeling and overloading of functions:

A) The unique identifier of a function in C + +--the function signature, which includes not only the name of the function but also the list of parameters ( not including the return value ).

b) A few elements that make up a function Overload:

Function name (contains the class name).

Formal parameter list (type and number of parameters).

The Const property (the member property of the Class).

5.IO Stream:

A) Io in C + + has three states: bad, fail, EOF. Take CIN for example:

If illegal data is entered, the fail of CIN is set to 1.

If the input is finished, the fail and EOF of CIN are set to 1.

b) repair of IO stream:

If this is the end of the file, simply call the clear function.

In the case of illegal data, in addition to clear, the illegal input needs to be emptied ( if not emptied, the data will remain in the buffer ).

Call the following function:

' \ n ');

6. Class:
A) A class is divided into two parts, data and functions ( data can be regarded as the properties of an object, and functions can be seen as the behavior of an object ).

b) The member functions of the C + + class have an implicit parameter that points to the pointer to the object that called the function , which is the this pointer.

c) Constructors can also be overloaded.

D) The statement in the initializer is the initialization of the variable, and the statement in the constructor block is the assignment of the variable .

e) The initialization order in the initialization list is related to the order in which variables are defined in class, regardless of the order in which they are declared in the initialization list .

f) Use initialization lists whenever possible instead of assignment in the function body

    when a member of class can only be initialized and cannot be assigned (for example, a const member or a reference member), we must use the initialization list .

    We must use the initialization list when the initialization of a member in class must be controlled manually by us and not given to the system default initialization (usually because the member does not have a default constructor) .

g)No access designator is written in class, default is private, and struct defaults to public. This is the only difference between the two .

h) Friend declaration in C + + is a one-way relationship. For example, a statement friend Class B means that B is a friend of a, can access the private data of a, but the reverse is not true.

7. Sequential containers:

A) to initialize another container with one container, requiring the exact same type ( the same container type and the same element type ).

b) Use the iterator scope to initialize the container, requiring only the iterator element to match the container element type ( not requiring the same container type ).

c) Any incoming iterator as a parameter of the specified range can be substituted with a pointer .

D) Any element that is placed in a vector must require the ability to copy and assign values .

e) Vector iterators continue to work unless:

The user inserts or deletes an element at a smaller index position.

Memory reallocation due to changes in capacity.

f) delete elements with erase remember to receive the return value, preferably using a while loop .

g) Several capacity-related functions of vectors:

A size () that represents the number of elements.

Resize (), adjusts the number of elements.

Capacity (), which indicates the number of seats that can be accommodated.

Reserve (), adjust the capacity.

h)the growth of vector memory is exponentially increasing .

i) the difference between vector and list:

Vectors are implemented using arrays, and lists are implemented with lists.

Vector support random access, List does not provide subscript

Adding a large number of deletions is appropriate for using list. and set:

A) pair is not a container, but instead represents a Key-value key-value pair.

b) Map is a container to store the pair object, but the storage method is different from the vector, the map uses a two-fork sort tree to store the pair, which is generally a red-black tree .

c) When the map is accessed using subscript, if key does not exist, a new pair,value is added to the map to the default value.

d) The key of map must have a less than operator operator<.

e) when inserting a map element with insert, the original value is not updated if it fails .

f) Comparison of map and set:

Both are implemented using red and black trees.

Key needs support < operation.

Map focuses on Key-value Quick Find.

Set focuses on seeing if an element exists.

g)None of the elements in map and set can be sorted .

9.reverse iterators:

A) Logically, Rbegin points to the last element, Rend points to the previous position of the first element.

b) on the actual implementation, Rbegin points to the next position of the last element, and rend points to the first element.

c) The physical location of the reverse iterator is 1 higher than the logical position.

D) The advantage of using this implementation is that the iterator is converted to the interval after reverse_iterator, which is the opposite of the previous interval, but the content is the same.

e) Reverse iterators cannot be used with erase functions. The correct way to delete is:

string:: Reverse_iterator (S.erase ((++it).  Base()));

10. Deep copy and shallow copy:

A) classes that contain pointer member variables have two choices when replicating:

Copy the value of the pointer so that after the copy is complete, two objects point to the same resource, which is called a shallow copy .

Copy the resource pointed to by the pointer, after copying, two objects each have their own resources, this is called deep copy .

b) Assignment operators need to release previously held resources and must handle the problem of their own assignment .

c) The copy constructor, the assignment operator, and the destructor, called the three rules , must provide an additional two once one is provided. Take string for example:

Involves a deep copy, shallow copy problem, so a copy constructor is required.

To be consistent, the assignment operator should also implement a deep copy.

Now that the deep copy is implemented, the resource (memory) must be applied, so the destructor frees the resource manually.

d) an empty class, the compiler defaults to provide parameterless constructors, copy constructors, assignment operators, and destructors, altogether four functions .

E) A method that disables the ability to copy and assign a class:

Set the copy function and the assignment operator to private.

Only declared, not implemented.

f) replication and assignment must be guaranteed to be consistent in the semantics of the program .

g) If a class does not need to be copied and assigned, disabling this capability avoids a large number of potential bugs.

h) If a class implements the ability to copy and assign values like value (meaning that after copying and assigning two objects without any association or logically seemingly unrelated), the object of this class is called the value semantics ; If a class cannot replicate, or if the resource attribution between objects is tangled, it is called object semantics, or referential semantics .

Some key notes in c++--Basic learning

Related Article

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: 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.