Pairs definition
Class pair can treat two values as one unit. Map and multimap are managed through pair.Key value/real value (key/value).
In the pair definition:
Namespace std {
Template <class T1, class T2>
Struct pair {
// Member
T1 first;
T2 second;
...
// Copy construct with implicit conversions
Template <class U, class V>
Pair (cosnt pair <U, V> & p)
: First (p. first), second (p. second ){
}
};
...
}
We can see that the template is used in the copy constructor.Implicit conversion required. If it is of the same type, instead of calling this constructor, it calls the copy constructor synthesized by the system.
For the method implemented in the above Code, in the study note 1 (http://www.wutianqi.com /? The second point in p = 2025.
Generate a convenient Pair function make_pair ()
Namespace std {
// Create value pair only by providing the values
Template <class T1, class T2>
Pair <T1, T2> make_pair (const T1 & x, const T2 & y ){
Return pair <T1, T2> (x, y );
}
}
In this way, you can use:
Std: make_pair (100, "TankyWoo ");
To replace:
Std: pair <int, string> (100, "TankyWoo ");
However,If a formula explicitly specifies a type, it has an advantage: the generated pair will have an absolutely clear type.For example:
Std: pair <int, float> (100, 8.88 );
And
Std: make_pair (100, 8.88)
It is not a type, because in the second element, the former is of the float type, while the latter is of the double type (float type by default ).
Class auto_ptr
Auto_ptr is a pointer that refers to an objectOwner ).Therefore, when an owned auto_ptr is destroyed, the object is also destroyed. Auto_ptr requires that an object only has one owner.
For example:
Void f ()
{
ClassA * ptr = new ClassA;
...
Delete ptr;
}
If... If an exception or return statement occurs in the Code, the ptr cannot be released, resulting in memory loss.
Void f ()
{
Std: auto_ptr <ClassA> ptr (new ClassA );
...
}
This will solve the problem.
Note: auto_ptr <> the assign initialization method that is commonly used by pointers is not allowed,You must use direct initialization..
Eg.
Std: auto_ptr <ClassA> ptr1 (new ClassA); // OK
Std: auto_ptr <ClassA> ptr = new ClasssA; // Error
Auto_ptr Ownership Transfer
The object ownership is handed over through the copy constructor of auto_ptr and the assignment operator.
Eg.
// Transfer ownership from ptr1 to ptr2
Std: auto_ptr <ClassA> ptr2 (ptr1)
Or
Std: auto_ptr <ClassA> ptr2;
Ptr2 = ptr1;
If ptr2 has an object before the assignment, delete is called to delete the object during the assignment.
Because auto_ptr will hand over ownership when assigning values, do not use auto_ptr in the function parameter list, or use it as the return value.
You can pass auto_ptr through reference.
Constant auto_ptr can reduce "inadvertent transfer ownership ". It means that you cannot change the ownership of auto_ptr, but you can change the objects owned by auto_ptr.
Eg.
Const std: auto_ptr <int> p (new int );
Std: auto_ptr <int> q (new int );
* P = 42; // OK
* P = * q; // OK
P = q; // Error
The last instance:
# Include <iostream>
Using namespace std;
Template <class T>
Ostream & operator <(ostream & strm, const auto_ptr <T> & p)
{
If (p. get () = NULL ){
Strm <"NULL ";
}
Else {
Strm <* p;
}
Return strm;
}
Int main ()
{
Auto_ptr <int> p (new int (42 ));
Auto_ptr <int> q;
Cout <"after initialization:" <Endl;
Cout <"P:" <p <Endl;
Cout <"Q:" <q <Endl;
Q = P;
Cout <"after assigning auto pointers:" <Endl;
Cout <"P:" <p <Endl;
Cout <"Q:" <q <Endl;
* Q + = 13;
P = Q;
Cout <"after changed and assignment:" <endl;
Cout <"p:" <p <endl;
Cout <"q:" <q <endl;
System ("PAUSE ");
Return EXIT_SUCCESS;
}
Output:
After initialization:
P: 42
Q: NULL
After assigning auto pointers:
P: NULL
Q: 42
After changed and assignment:
P: 55
Q: NULL
Three auxiliary functions: min (), max (), and swap ()
All are defined in the header file <algorithm>
The header files <cstddef> and <cstdlib> define common constants, macros, types, and functions.
Definition in <cstddef>
NULL Pointer value, indicating "undefined" or "undefined"
Size_t is a type without plus or minus signs, used to indicate the size (such as the number of elements)
Ptrdiff_t is a type with positive and negative numbers, used to indicate the distance between pointers.
Offsetof indicates the offset of a Member in struct or union.
<Cstdlib>
Exit (int status) exit (exit, exit) Program
The EXIT_SUCCESS program ends normally.
EXIT_FAILURE program ended abnormally
Abort () Exit the program (may cause a crash on some systems)
Atexit (void (* function) () calls some functions when exiting the (exit) program.
Next we will introduce offsetof:
Offsetof:
Retrieves the offset of a member from the beginning of its parent structure.
Size_t offsetof (structName, memberName );
Parameters:
StructName: Name of the parent data structure.
MemberName: Name of the member in the parent data structure for which to determine the offset.
Return Value: offsetof returns the offset in bytes of the specified member from
The beginning of its parent data structure. It is undefined for bit fields.
Remarks:
The offsetof macro returns the offset in bytes of memberName from the beginning of the structure specified by structName. You can specify types with the struct keyword.
Note:
Offsetof is not a function and cannot be described using a C prototype.
Here is an example:
// Author: Tanky Woo
// Blog: www.WuTianQi.com
# Include <cstddef>
# Include <iostream>
Using namespace std;
Typedef struct TK {
Char;
Int B;
Char c;
} TK;
Int main ()
{
Cout <offsetof (TK, a) <endl;
Cout <offsetof (TK, B) <endl;
Cout <offsetof (TK, c) <endl;
System ("PAUSE ");
Return EXIT_SUCCESS;
}
Memory alignment is involved in this article. I have posted the following articles about memory alignment in other places:
This is because the blog garden has disabled the website. You can go to the original article of this article to see the memory alignment URL.
Original: http://www.wutianqi.com /? P = 2036