C++

Source: Internet
Author: User

# # #指针和引用的区别
1. The pointer is a variable that stores the address pointing to the data, and the reference is simply an alias of the original variable, which is essentially the same as the original variable.
2. Pointers can be multilevel, reference can only have one level
3. The pointer can be NULL, the reference cannot be empty, and must be initialized when it is created
4. The pointer can be const, but the reference does not have a const//should be changed to a pointer can have two-layer const, reference only one layer
5. The value of the pointer can be changed after initialization, the referenced value can not be changed
6. sizeof refers to the size of the variable, and the sizeof pointer gets the size of the pointer itself
7. The pointer and the app's self-increment + + operation have different meanings

# # #指针, references
1. The reference must be initialized, the reference is not an object, there is no address space, so there is no pointer to the reference.
2. The symbol closest to the variable name has the most direct effect on the type of the variable, such as int * &a; A is a reference and a reference to an int pointer;
3. Constant pointer: A pointer to a constant, the pointer itself can be changed, but the object pointed to cannot be changed. int const * p; or const int * p;
4. Pointer constant: The pointer itself is a constant, the pointer itself is immutable, the object pointed to can be changed. int * const P;
5. Pointer constant to constant: that is, the pointer itself is constant, immutable, and the object pointed to cannot be changed. const int * const P;

# # #string, Vector
1. The initial value of the list is also the number of elements, depending on whether the initial value is passed with curly brackets or parentheses.
2. You cannot add an element to a vector in a range for loop or in a loop that uses an iterator.
3. Any operation that could change the capacity of a vector object, such as Push_back, would invalidate the vector's iterator
4. Two vectors are equal if and only if they contain the same number of elements, and the corresponding position of the element value is the same.
5. Vectors can use subscript operators to access existing elements, not to add elements, and to add elements with push_back.

1. Use static inside the function to make the life cycle of the local variable run through the function call and after the time, the scope is unchanged.
2. When an argument is initially taxiing, the top-level const is ignored, and whether the parameter is passed to a const type can be
3. The array has two properties: it is not allowed to copy arrays and to use arrays when they are usually converted to pointers. So when you pass an array to a function, you actually pass a pointer to the first element of the array.
4. The function overload does not depend on the return value. The combination of function names and parameters determines which functions are used, regardless of the return value. If the return value is different and the other elements are the same, it is an error.
5. A formal parameter with a top-level const cannot be distinguished from a formal parameter without a top-level const, and cannot be overloaded.
6. However, if the parameter is a pointer or reference, it can be overloaded with const, at which point the Const is the underlying. If the argument is const, the non-const version must be selected with the const version of the function instead of the Const.
7. Specify the default argument in the function declaration, put it in the header file, and the subsequent function definition does not need to write the default argument.
8. Inline functions avoid the overhead of function calls, and the inline function definition is placed in the header file.

# # #IO流
1. IO objects are not copied and can only be used for return values and parameters of functions using references
2. End of File: S.eof (); Recoverable error: S.fail (); System-Level Error: S.bad (); Indicates that the stream is in a valid state: S.good (); Cin.clear () Clears the error flag bit,
3. NewLine flush buffer Endl, flush buffer flush, flush buffer ends after inserting null character;
4. Flush the buffer operator unitbuf each time, and revert to normal use of nounitbuf. such as cout << unitbuf for immediate refresh
5. Stream yourself Empty buffer: Cin.ignore ()
6. C + + IO stream efficiency is low, object-oriented, and input and output with virtual function, more overhead.

1. Functions defined within a class without inline are implicit inline functions
2. This pointer is a pointer constant that always points to itself and cannot be changed
3. When providing an initialization value within a class, you must use the symbol = or curly braces to indicate
4. Return *this, if you return a reference to the object itself, you can manipulate the object through a string of statements, and return a non-reference to pass the copy without achieving this effect.
5. Const-based overloading: You can overload a member function with two const and non-const versions of the function, allowing the compiler to find the appropriate
6. The benefits of defining small functions: Avoid using the same code in multiple places; we expect these small functions to become very complex in the future, so that the modifications will be convenient; adding and removing debugging information only needs to be done in one place; the small definition of inline functions does not bring any additional overhead.
7. Friend relationship between classes: Defines a friend class, and member functions of the friend class can access all members of this class.
8. Friend relationships do not have transitivity, each class is responsible for controlling its own friend or friend function
9. Declare the scope of the friend function and the friend function declaration scope is by the difference, must be in its own declaration scope to prevail
10. If you are using a const, a reference, or a class type that does not provide a default constructor, you should provide the initial value through the constructor's original list
11. On the one hand initialization and assignment are different in the underlying efficiency, and more importantly, some members must be initialized. You should develop the habit of using the initial values of the constructor.
12. The order of initialization lists is related to the order in which variables in the class are declared, regardless of the order in which the lists are initialized. It is best to keep the order of the constructor's initial values consistent with the order of the member declarations


# # #static的用法
# # #面向过程程序设计中的static
# # # # #1. Static global Variables
Allocates memory in the global zone, is automatically initialized to 0, is not visible outside the file that declares it.
# # # # #2. Static local Variables
Allocates memory in the global zone, is automatically initialized to 0, the life cycle ends from the declaration to the program, but its scope is within the function that declares it. The second static local variable is used only by this function, which is equivalent to a global variable dedicated to this function.
# # # # #3. Static functions
Static functions are defined in front of the function name and can only be used in the file in which it is declared.

# # #面向对象的static
# # # #静态数据成员: Static data members within a class are declared before the declarations of the data members within the classes.
1. Non-static members each object has its own copy. Static data members have only one copy in the program, shared by all objects of that class. However, it does not belong to a particular class and can be manipulated without any class instances.
2. Stored in the global data area. You cannot initialize in a class declaration because you are allocating space when you define it. Initialize outside the class, no more static keywords are required for initialization. Format:< Data type > < class name >::< static data name > = < value >
3. Although public, protected, private access rights are also respected, they cannot be accessed outside the class. There are two formats for accessing static Members:< class object name >.< static member name >; if static member is public, you can < class name >::< static data name >
4. Static data members are primarily used when each object has the same property. One is to save storage space, the second is to modify, only need to modify a place. For example, interest in deposit classes.
5. Static data members have two benefits compared to global variables. One is that there is no global namespace to enter the program, no conflicts with other global variables, and the second is to implement information hiding, which can be defined as private, and global variables cannot.

# # # #静态成员函数
Like a static data member, a static member function is not a service to a specific object in a class, it is a full service for a class.
1. The internal default of the normal member function hides the this pointer to the object itself, and the static member function does not have the this pointer. Therefore, it cannot access non-static data members and non-static member functions of class objects, only static member functions can be called.
2. Static members can access each other, including static data members and static member functions.
3. Because there is no additional overhead for this pointer, its speed is slightly elevated compared to the global function of the class.
4. Objects of the class can be called directly with a static member function, or you can access the:< class name >::< member functions name > (< parameter table >) when there is no class object


# # #顺序容器: vector, deque, list, forward_list, array, string
Provides the ability to control the order in which elements are stored and accessed. This order does not depend on the value of the element, but instead corresponds to the position of the element when it joins the container.
# # #优缺点
1. String and vector: Random access is fast, but insertions and deletions are time consuming;
2. List (doubly linked list) and forward_list (one-way list): Inserting and deleting data is fast, but random access is not supported and memory overhead is large.
3. Deque (double-ended queue): Support random access, insert and delete elements at both ends fast, but in the middle insert and delete slow
4. If there is a better reason to choose another container, use a vector. (Use list also more common)

# # #操作
1. Insert inserts an element into the position specified by the iterator before C.insert (p, t) inserts t before P refers. You can also insert a range element after the pointer, or you can use the return value of insert to insert the element repeatedly at the same location.
2. List, forward_list, deque and Push_front operations, you can insert elements into the table header
3. Swap operation swaps the contents of two containers of the same type. Swap (A, b) or A.swap (b). In addition to array, swap does not copy, delete, or insert any elements, it can be done in constant time, and pointers, references, and iterators that point to the container are not invalidated except for string, but only those elements that point to the pre-swap are thrown. The swap operation of the array will actually swap the elements.
4. Member access: at and [] apply only to string, vector, deque and array, using at if out of bounds, will throw Out_of_range exception, back does not work with forward_list. The front, back, and so on are returned as references. Calling front and back on an empty container is a serious error, just like using an out-of-bounds subscript.
5. Erase and insert operations are similar, deleting an element function does not check the parameters, and you need to make sure that they exist. This can also cause the iterator to fail.
6. Initialize: Only the constructor of the sequential container accepts the size parameter, and the associative container is not supported; You can initialize the standard container with an iterator scope. c C (b, E) Initializes C with an element between B and E.
7. Change the container size and management capacity: Resize (n) Change the size of the container, if the changed value is less than size () will discard the n element; reserve (n) alters the container's capacity and does nothing if it is less than n; capacity () returns the current capacity size.

# # # #额外的string操作
The functions in string are made up of multiple overloaded versions, and the appropriate version of the function is used according to the actual situation.
1. substr (pos, N) string operation, append () appended at the end, replace () replacement operation is equivalent to erase after the Insert;find () function to find the position of the string in the main string, reverse lookup; compare () Similar to the STRCMP function in C language.
2. Numeric conversion function: to_string (); Converting a value to String,val can be any arithmetic type. There are a number of other functions that convert a string to a numeric value.

# # #迭代器
1. The concept of iterators is the basis of the standard library
2. The iterator uses a left-closed-right-open interval, where begin points to the first element, and end points to the next position of the last element. Such programming assumes that ++begin can be used with confidence to reach end.
3. Assignment-related operations result in the invalidation of iterators, references, and pointers inside the container on the left. (Swap operation not)
4. For adding elements to iterators, removing elements from iterators, resize, assignments, SWAPD, and so on, can cause iterators to fail, so you must ensure that you reposition the iterators correctly after each change to the container. This is especially important for vectors, strings, and deque.
5. Do not cache the value of. End (), and End () operates quickly.

# # # #适配器Adaptor: Stack, queue, priority_queue
Essentially, an adapter is a mechanism that can make something look like another thing.

# # #关联容器associative-container:map, set
The elements of the associated container are saved and accessed by keyword.
1. The callable object passed to the sort () function must satisfy the same requirements as the keywords in the associated container;
2. Strict weak sequence (strict weak ordering): Two keywords can not be less than equal to each other; if K1 is less than or equal to K2, and K2 is less than or equal to K3, then K1 must be less than or equal to K3; If two keywords, any one is not less than equal to each other, is equal.
3. The element in set is const, the element in map is the first element in Pair,pair is const, so the associative container can be used to read only the elements of the algorithm. But the associative container's own algorithm is more useful than calling generics.
4. The return value of the insert () operation for map is an iterator that pair,pair.first to the given keyword, pair.second is a bool value, indicating whether the insert succeeded.
5. The subscript operator of map may insert a new element, using subscript or at operations only for non-const maps. The at () operation throws a Out_of_range exception if the keyword does not exist. And unlike string and vector, the return type of the subscript operator differs from the type that the iterator references. The subscript operator returns Mapped_type, and the dereference iterator returns value_type

C++

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