C Language Section
1. Analysis of pointers
int (*p) [5] and int *p[5]
The former is a pointer to an array that contains 5 elements. The latter is an array with a length of 5, and each element in the array points to an integer variable.
int *f (int i, int j) and Int (*p) (int i, int j)
The former is a function that returns a pointer, which is the declaration of a function, which is a pointer to a function that defines a pointer.
The role of 2.C language static.
? In the function body, a variable declared as static maintains its value in the process of the function being called.
? Within the module (but outside the function body), a variable declared as static can be accessed by a function within the module, but not by other functions outside the module. It is a local global variable.
? Within the module, a function declared as static can only be called by other functions within this module. That is, this function is restricted to the local scope of the module that declares it.
The role of 3.extern "C".
Implement mixed programming of C + + and C and other languages.
In the case of int func (int, int), the compiler of C will adapt the name to _func, whereas the C + + compiler will be adapted to _func_int_int or _funcii (different compilers).
If this function is compiled into a library in C, the function name in the target file is _func, when the function is called in C + +, the C + + compiler will go to the target file to find _funcii, the result is not found, error.
Therefore, in order to prevent this problem, in the C + + call, add an extern "C" before the function declaration to the C + + compiler, do not re-modify the name, and go directly to find _func.
The difference between 4.strcpy,strncpy.
are copy functions of strings, first look at function prototypes
char * strcpy (char * destination, const char * source);
The memory areas referred to by SRC and dest cannot overlap and dest must have enough space to accommodate the SRC string. Returns a pointer to the dest.
char * strncpy (char * destination, const char * source, size_t num);
The third parameter represents the maximum number of strings copied from source.
Copies the first n bytes of the string referred to by SRC to the end of ' dest ' into the array referred to by the.
If the first n bytes of Src do not contain a null character, the result does not end with a null character.
If the src length is less than n bytes, the dest is padded with null until n bytes are copied.
The memory areas referred to by SRC and dest cannot overlap and dest must have enough space to accommodate the SRC string.
Returns a pointer to the dest.
5. Manually implement the strcpy.
char * strcpy (char * destination, const char * source) {if (destination = = NULL | | source = NULL) return Null;char *p = d Estination;while ((*destination++ = *source++)! = ' + '); return p; }
C + + Language section
1. The difference between a pointer and a reference
1). The pointer is an entity, and the reference is only an individual name;
2). Reference is used without dereference (*), the pointer needs to be dereferenced;
3). References can only be initialized once at the time of definition, and then immutable; pointers are variable;
4). The reference does not have a const, the pointer has a const;
5). The reference cannot be null, the pointer can be empty;
6). "SizeOf Reference" gets the size of the variable (object) pointed to, and the "sizeof pointer" gets the size of the pointer itself (the address of the variable or object to which it is pointing);
7). Pointer and reference self-increment (+ +) operation has different meanings;
8). From memory allocation: The program allocates memory regions for pointer variables, whereas references do not need to allocate memory areas. )
2. When the copy constructor is called, what is a dark copy.
An object is passed in as a value to the body of the function.
An object is returned from the function in the way that the value is passed.
An object needs to be initialized with another object.
In some cases, in-class member variables need to dynamically open up heap memory, if a bit copy is implemented, that is, the value of the object is completely copied to another object, such as A=b. At this point, if a member variable pointer in B has already applied for memory, that member variable in a also points to the same piece of memory. There is a problem: when B releases the memory (for example, a destructor), a pointer inside a is a wild pointer, and a run error occurs.
A shallow copy refers to a simple assignment of data members in an object when the object is copied, and the example above is a shallow copy, and the default copy constructor performs a shallow copy.
In the case of a deep copy, for dynamic members in an object, you cannot simply assign a value, but you should reallocate the space dynamically.
The implementation of the 3.String class.
String.cpp
#include <utility>//for std::swap#include <string.h>//for strcpyclass string{public:string (): M_data (new Char[1]) {*m_data = '} ';} String (const char* str) {if (str = = NULL) {m_data = new Char[1];*m_data = '} ';} Else{m_data = new Char[strlen (str) + 1];strcpy (m_data, str);}} String (const string &RHS) {if (RHS = = NULL) {m_data = new Char[1];*m_data = '} ';} Else{m_data = new Char[rhs.size () + 1];strcpy (m_data, Rhs.c_str ());}} ~string () {delete[] m_data;} size_t size () Const{return strlen (m_data);} Const char* C_STR () Const{return m_data;} string& operator= (const String &RHS) {if (this = other) return *this;delete[] M_data;m_data = new Char[rhs.size () + 1];strcpy (M_data, Rhs.c_str ()); return *this;} Private:char* m_data;}
4.STL of various containers in the bottom of the implementation and characteristics and use of the occasion.
Vector underlying data structures are arrays for fast random access
List underlying data structure is a doubly linked list that supports quick additions and deletions
Deque the underlying data structure is a central controller and multiple buffers, supporting both end-to-end (middle cannot) quick additions and deletions, also support random access
Stack bottom generally with 23 implementation, closed the head can be, the reason for not using vectors should be limited capacity size, time-consuming expansion
The bottom of the queue is generally implemented with 23, closed the head can be, the reason for not using vectors should be limited capacity size, the expansion time
45 is the adapter, not the container, because it is a re-encapsulation of the container
The underlying data structure of priority_queue is generally vector as the underlying container, heap heap is the processing rule to manage the underlying container implementation
Set underlying data structure for red-black trees, ordered, not duplicated
Multiset underlying data structure for red-black trees, orderly, repeatable
Map underlying data structure for red-black trees, ordered, not duplicated
Multimap underlying data structure for red-black trees, orderly, repeatable
Hash_set the underlying data structure is a hash table, unordered, non-repeating
Hash_multiset underlying data structures are hash tables, unordered, repeatable
Hash_map the underlying data structure is a hash table, unordered, non-repeating
Hash_multimap underlying data structures are hash tables, unordered, repeatable
Vectors, lists, and deque provide programmers with different complexities, so it should be used: vector is a type of sequence that can be used by default and should be used when inserting and deleting the middle of a sequence very frequently. This data structure can be selected deque when most insertions and deletions occur at the head or tail of a sequence.
Do you need the ability to "insert a new element anywhere in the container"? If it is, you need a sequence container, and the associated container does not.
Do you care about the order of the elements in the container? If not, a hash container is a viable option. Otherwise, you should avoid using hash containers.
What type of iterator do you need? If you must be a random-access iterator, technically you can only be limited to vectors, deque, and strings.
When inserting or deleting data, are you very concerned about the movement of existing elements within the container? If it is, you must discard the contiguous memory container.
Does the memory layout of the data in the container need to be compatible with C? If it is, you can only use vectors.
Is it important to find speed? If so, you should look at the hash container (better than) the sort of vector (better than) the standard associative container is probably in this order.
Do you need to insert and delete transactional semantics? In other words, do you need to have the ability to reliably rewind insertions and deletions? If so, you will need to use a node-based container. If you need transactional semantics for multiple element insertions, you should choose List, because list is the only standard container that provides the transactional semantics of multivariate insertion. Transactional semantics are important for programmers who are interested in writing exception-safe code.
Do you want to minimize the number of failures for iterators, pointers, and references? If so, you should use node-based containers because insertions and deletions on those containers do not invalidate iterators, pointers, and references (unless they point to elements you delete). In general, inserting and deleting on contiguous memory containers invalidates all iterators, pointers, and references that point to the container.
Do you need a sequence container with the following characteristics: 1) You can use a random-access iterator, 2) as long as the insert is not deleted and the insertion only occurs at the end of the container, the pointer and the referenced data do not expire? This is a very special situation, but if you encounter this situation, deque is the container of your dreams. (Interestingly, when inserting only at the end of a container, the deque iterator may also fail, and deque is the only standard STL container that "does not invalidate its pointers and references when the iterator fails.") )
5. const before function and const after function
The previous use of Const means that the return value is const, and if the function return value is in "value passing", the const modifier has no value since the function will copy the return value into the external temporary storage unit. The contents of a function return value (that is, the pointer) cannot be modified if the return value of the function returned by the "pointer Pass" method is added to the const modifier, and the return value can only be assigned to a const-decorated pointer of the same type.
The following const is a const member function that is const in both the function definition and the function declaration.
Const data members and const member functions
The const-decorated data member of a class, which represents a member constant, cannot be modified, and it can only be assigned to a value in the initialization list.
The const member function should add a const qualification to both the function prototype description and the function definition.
For const class objects/pointers/references, only const member functions of the class can be called, so the most important role of the Const modifier member function is to restrict the use of Const objects.
The const member function is not allowed to modify any of the data members of the object to which it resides.
The const-Decorated class object indicates that the object is a constant object, and any of its members cannot be modified. The same is true for object pointers and object references.
Usage of const
· The bold use of const will bring you endless benefits, but only if you have to figure it out;
· To avoid the most general assignment error, such as assigning a const variable to the value;
· Using const in parameters should use a reference or pointer instead of a generic object instance for the same reason;
· The three usages of const in member functions (parameters, return values, functions) should be used very well;
· Do not easily set the return value type of the function as const;
· In addition to overloading operators, it is generally not to make the return value type a const reference to an object;
· Any function that does not modify a data member should be declared as a const type.
6.c++ four types of conversion characters.
Const_cast, the literal understanding is to go to the const attribute.
Static_cast, the name is understood to be a static type conversion. such as int converted to char.
dynamic_cast, the name is understood to be a dynamic type conversion. such as a polymorphic type conversion between a subclass and a parent class.
Reinterpreter_cast, only the type is re-interpreted, but there is no binary conversion.
7.volitale keywords
If a basic variable is modified by volatile, the compiler will not save it to the register, but each time it accesses the location in memory where the variable is actually saved. This avoids the catastrophic problem caused by compiler optimizations in the multi-threaded read and write of variables without volatile modifiers. Therefore, you must add the volatile modifier to the basic variables that you have to share in multiple threads. Of course, volatile also allows you to capture non-thread-safe code at compile time.
8.explicit keywords
Explicit can effectively prevent errors or misunderstandings caused by implicit conversions of constructors.
9. Describe the use of the static keyword.
The static keyword can be used to declare variables, functions, class data members, and class functions, primarily to control how variables are stored and visible.
Inside the class
The declaration of a data member in the class body is preceded by the static keyword, which becomes a data member of that class. As with other data members, static data members also adhere to public/protected/private access rules. At the same time, static data members:
1). Static data members are actually global variables in the class domain. Therefore, the definition of the static data member (initialization) should not be placed in the header file;
2). Static data members are shared by all objects of the class, including objects of the class's derived classes. That is, the derived class object shares the static data members of the base class with the base class object;
3). Static data members can be optional parameters of member functions, while ordinary data members are not;
4). ★ The type of the static data member can be the type of the owning class, while the normal data member cannot. A normal data member can only be declared as a pointer or reference to the owning class type;
5). The static member of the class can be changed in the ★const function. Cannot change ordinary members;
#include <iostream>using namespace Std;class base{public: base () {_i=0;_val=0;} mutable int _i; static int _staticval; int _val; void test () const{//const function member _i++;//right,mutable member _staticval++;//right,static member _val++;//error:increment of member ' Base::_val ' in Read-only object }}; int Base::_staticval=0;int Main () { cout<< "hello!" <<endl; return 0;}
Static member functions:
A static member function is a ready-to-use instance of a class that provides access to a static member function for the class's entire object. So there is no need to define a global function, which has the following characteristics
1). The address of a static member function can be stored using a normal function pointer, while the ordinary member function address needs to be stored with a class member function pointer;
2). Static member functions cannot call non-static members of a class. Because the static member function does not contain the this pointer;
3). Static member functions cannot be declared at the same time as virtual, const, volatile functions;
Memory allocation
A static member variable of a class is like a static variable in a function that allocates memory at compile time and stores it in the data segment until the program exits and is not released as the object is deleted.
In the C language, static is a little bit different.
Static variable:
1). The variable is placed in the program's global store so that the original assignment can be maintained at the next call. This is the difference between a stack variable and a heap variable.
2). Variables tell the compiler with static that they are visible only within the scope of the variable. This is the difference between it and the global variable.
static function
1). Static functions are automatically assigned to a storage area that has been in use until the application instance is exited, avoiding the push-stack out of the calling function, much faster.
2). The static function (also called an intrinsic function) can only be called by functions in this file, not by functions in other files in the same program.
The Sword is xx games (ix)-C + + must know