A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service
Summary of the first part of the STL standard library (Auto_ptr & numeric_limits, standard template library, iterator adapter), stlauto_ptr
1 auto_ptr ownership transfer (auto_ptr requires that only one object can have only one owner, rigorous, and unique)
Std: auto_ptr <ClassA> ptr1 (new ClassA)
Ptr1 has new objects
Std: auto_ptr <ClassA> ptr2 (ptr1)
After ptr2 gives control to ptr2, ptr2 will have a new object and ptr1 will not have it.
2. Implemented by assigning values
Std: auto_ptr <ClassA> ptr1 (new ClassA );
Std: auto_ptr <ClassA> ptr2;
Ptr2 = ptr1;
By assigning values, the ownership is transferred from ptr1 to ptr2, so ptr2 has the object previously owned by ptr1. If ptr2 is having another object before being assigned a value, when a value assignment occurs, delete will be called to delete the object. [the previous ptr1 lost ownership. After the ownership is handed over, there will be two empty pairs with only null pointers in hand]
NOTE 2: Only auto_ptr can be used as the initial value of another auto -- ptr. Normal pointers do not work.
3. Use of ownership transfer
A function can use auto_ptr to forward ownership to another function:
(1) A function is the end of data. If auto_ptr is passed to a function as a parameter in the pass-through mode, the call end obtains the ownership of auto_ptr. If the function is not passed out, it will be deleted when the function is exited.
Void sink (std: auto_ptr <ClassA>)
(2) A function is the starting point of data. When an auto_ptr is returned, its ownership is transferred to the caller.
(3) by using the constant reference, you can use another auto_ptr to deliver control permissions when passing values to a function.
Voidbad_print (std: auto_ptr <T> p)
If (p. get () = NULL)
Cout <"NULL" <endl;
Std: cout <* p <endl;
Void f ()
Conststd: auto_ptr <int> p (newint); // defined as const type
* P = 42;
Bad_print (p); // compilation error will occur here
* P = 48; // No error will occur here
The const keyword does not mean that you cannot change the object owned by auto_ptr, but means that you cannot change the ownership of auto_ptr.
4 precautions for auto_ptr
(1) auto_ptrs cannot share ownership.
(2) There is no auto_ptrs designed for array.
(3) Auto_ptrs is not a "universal" smart pointer
(4) Auto_ptrs does not meet the requirements of STL containers for other elements
1 C ++ Standard Library provides extreme values through template numeric_limits
The Integer constants in C language are defined in <climits> and <limits. h> floating point constants are defined in <cfolat> and folate. h
The minimum length of the built-in type in:
Char 1 byte
Short int 2 byte
Int 2 byte
Long int 4 byte
Float 4 byte
Double 8 byte
Long double 8 byte
2 numeric_limits not only provides a general-purpose template, but also provides its specific version
(1) A universal template that provides default extreme values for all types
(2) various extreme values are provided by the special version.
(3) Template <> classnumeric_limits <int>
Static const bool is_specialized = true;
Static T min () throw ()
Here, is_specialized is set to true. All other Members are set based on the extreme values of specific types.
Cout <boolalpha <endl;
Cout <"max (short)" <numeric_limits <short>: max () <endl;
Cout <"max (long)" <numeric_limits <long>: max () <endl;
Cout <"string is_specialized" <numeric_limits <string>: is_specialized () <endl; // determines whether to define the numerical limit.
<Cstddef> and <cstdlib>
Definitions in <cstddef>
Null is also defined in <cstdio> <cstdlib> <cstring> <ctime> <cwchar> <clocle>
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 signs used to indicate the pointer distance.
Offsetof indicates the offset of a Member in struct or union.
Definitions in <cstdlib>
EXIT_SUCCESS and EXIT_FAILURE can be used as the exit () parameter or the return value of main ().
Eixt (intstatus) exit (exit) Program (and clear static objects)
Abort () Exit the program (may cause a crash on some systems)
Atexit (void (*) function () calls a function when exiting the program.
5. C ++ Standard Template Library)
1 STL component creation)
Key components: Containers, iterators, and Algorithms
The basic concept of STL is to separate data from operations. Data is managed by the container class, and operations are defined by customizable algorithms. The iterator acts as an adhesive between the two, allows any algorithm to interact with any container
2 Containers and iterators
1 bidirectional iterator:
It can be carried out in two directions to advance with incremental operations or to return with the decreasing operator (list set multiset map multimap provides such iterator)
2 random deposit iterator:
The Random Access iterator has all the attributes of the two-way iterator and the random access capability: vector deque string
For (pos = coll. begin (); pos! = Coll. end (); ++ pos)
For (pos = coll. begin (); pos <coll. end (); ++ pos)
Only the random access iterator supports operation <; therefore, the code is generally written as the first
Reverse () to flip the range Energy Element
Coll2.resize (coll. size (); // change the number of coll2 Elements
Deque <int> coll3 (coll1.size (); Specify sufficient space during initialization
Six iterator adapter:
(1) Three iterators:
1 Insert iterators (Insert iterator)
2 stream iterators (stream iterator)
3 Reverse iterators (Reverse iterator)
Inset iterators can enable the algorithm to operate in a plug-in mode rather than a overwriting mode to solve the "insufficient target space" problem of the algorithm. Yes, it will increase the size of the target interval as needed.
If you set a value for an element, the cluster will be inserted. The insertion location is at the beginning of the container or at the end of a specified position, depending on the three conditions:
(1) One step forward will not cause any movement
1.1 Back inserters (installed at the tail end of the container)
Back inseters internally calls push_back () to insert an element at the end of the container
Copy (coll1.begin (), coll1.end (), back_inserter (coll2)
(Only the vector deque list provides push_back ())
2.2 Front Inserters)
Copy (coll1.begin (), coll1.end (), front_inserter (coll3 ));
This action reverses the order of inserted elements. If you plug 1 first and then plug 2 forward, then 1 will provide the push_front container behind 2 only: deque list
2.3 Generalinerters (General Plug-in ):
In general, inserters is used to insert the element into the front inserters at the location where the second parameter is accepted during initialization. The inserters internally calls the member function insert () all STL containers provide the insert () function with new values and new positions as parameters.
(2) Stream Iterator (Stream Iterator)
Copy (istream_iterator <string> (cin), istream_iterator <string> (), back_inserter (coll ));
Sort (coll. begin (), coll. end ());
Unique_copy (coll. begin (), coll. end (), ostream_iterator <string> (cout, "\ n "));
2.1istream _ iterator <string> (cin)
This generates a streamiterator that can read data from the standard input stream cin.
2.2 istream_iterator <string> ()
Call the default constructor of istreamiterator to generate an iterator that represents the "stream end" symbol. It means that you cannot read anything from it.
2.3 ostream_iterator <string> (cout, "\ n ")
Generate an output stream iterator and use operator <to write strings to cout. The second parameter is used as the delimiter between elements.
(2) Reverse Iterator (Reverse Iterator)
Perform operations in reverse mode
All containers can generate a reverse iterator through the member functions rbegin () and rend ().
(2) easier algorithm (manipulatingalgorithm Algorithm for deleting, rearranging, or modifying elements)
1. remove () an element from a certain interval.
For (inti = 1; I <= 6; ++ I)
Coll. push_front (I );
Coll. push_back (I );
Remove (coll. begin (), coll. end (), 3 );
The value 3 is deleted and overwritten by the subsequent elements. As for the elements at the end of the cluster that violate the overwriting rules, the elements are not in the same group but logically those elements do not belong to this cluster.
List <int>: iterator end = remove (coll. begin (), coll. end (), 3)
Copy (coll. begin () end, ostream_iterator <int> (cout ,","));
Distance (end, coll. end ());
Distance () returns the Distance between two iterators.
If you really want to extract the deleted elements, you must call the corresponding member function container of the container to provide the member function erase () For this purpose erase () you can delete all elements in the "parameter range ".
2. Easy-to-use algorithms and related containers
The Erase () member function returns the number of deleted elements.
(3) using functions as algorithm parameters
Void print (intelem)
Cout <elem <"";
For_each (coll. begin (), coll. end (), print );
The so-called predicate is a function that returns boolean values. They are usually used to specify sorting rules and search criteria, it is not a valid predicate STL that returns a bool value to a unary function or binary function. Therefore, the same result must be obtained for the same value predicate, this rule removes the functions that "change their internal state when called"
3. functors (FunctionObject is the function object)
The "function parameter" passed to an algorithm is not necessarily a function, but an object that acts like a function.
Advantages of function imitation:
(1) intelligent vague smart point
The Imitation function can have member functions and member variables, which means that the imitation function has the state. Secondly, You can initialize them at the beginning, and of course they must be called before
(2) imitation functions all have their own types (function inheritance systems can be designed)
(3) imitation functions are faster than general functions.
Set <int> coll; is expanded to set <int, less <int> coll;
So the reverse arrangement of these elements can be set <int, greater <int> coll;
With some special function adapters, you can combine predefined functions and other numeric values or use special conditions.
Transform (coll. begin (), coll. end (), back_inserter (coll2), bind2d (mltiiplies <int> (), 10)
Multiply all elements in coll by 10 and insert them to coll2. Here, the adapter is used to make the multiple <int> operation use the element of the source cluster as the first parameter, and 10 as the second parameter.
The generic function is declared as inline.
For_each (coll. begin (), coll. end (), mem_fun_ref (& Person: save ());
The Imitation function mem_fun_ref is used to call a member function of the element to which it is applied.
(4) elements in the container
1. Container element Conditions
(1) You must use the copy constructor to copy data. The performance of the copy function is optimized as much as possible.
(2) The value assignment operation must be completed through the assignment operator (that is, the = sign)
(3) The Destruction action must be completed through the destructor. The Destructor cannot be designed as preivate.
The Destructor must not throw an exception.
(4) For a sequence container, the default constructor of the element must be available.
(5) operatpor = must be defined for certain actions to perform equal testing.
(6) In an associative container, elements must define the sorting criterion. By default, operator <calls through the less imitation function <>
2 value semantics and reference Semantics
All containers create a copy of the element and return the copy. This means that the elements in the container are equal to the objects you put in, but not the same.
Start building with 50+ products and up to 12 months usage for Elastic Compute Service