The use of a auto_ptr
1 auto_ptr Ownership Transfer (AUTO_PTR requires one object can only have one owner, rigorous one without two masters)
STD::AUTO_PTR<CLASSA>PTR1 (New ClassA)
PTR1 has the new object.
STD::AUTO_PTR<CLASSA>PTR2 (PTR1)
Give control to PTR2, and then PTR2 has the new object and PTR1 doesn't have it.
2 implementation by assignment
STD::AUTO_PTR<CLASSA>PTR1 (new ClassA);
std::auto_ptr<classa>ptr2;
PTR2=PTR1;
Transfer ownership from PTR1 to PTR2 by an assignment action so PTR2 owns the object previously owned by PTR1, and if PTR2 is assigned another object before the assignment occurs, it is called Delete to delete the object. The previous PTR1 lost possession of ownership after handing over two pairs of empty hands with null pointers in hand "
Note 2: Only auto_ptr can be used as the initial value of another auto--ptr, not the normal pointer.
3 usefulness of the transfer of ownership
A function can use AUTO_PTR to transfer ownership to another function:
(1) A function is the end point of the data if Auto_ptr is passed as a parameter to a function as a value, then the callee gets the ownership of this auto_ptr if the function is not passing it out, it will be deleted when the function is exited
Void Sink (std::auto_ptr<classa>)
(2) A function is the starting point of the data when a auto_ptr is returned, its ownership is transferred to the caller
(3) by using the constant reference can be another auto_ptr in the transfer of value to the function cannot surrender control
Template<classT>
voidbad_print (std::auto_ptr<T>p)
{
if (P. Get () = =NULL)
{
cout<<"NULL"<<endl;
}
Else
{
std::cout<<*P<<endl;
}
}
Void F ()
{
CONSTSTD::auto_ptr<int>p (newint);//defined as const type
*p=42;
Bad_print (P);//There will be a compilation error here
*p=48;//here will be no error
}
The keyword const does not mean that you cannot change the object owned by Auto_ptr, but that you cannot change the ownership of AUTO_PTR.
4 Auto_ptr Precautions
(1) Ownership cannot be shared between auto_ptrs
(2) There is no auto_ptrs designed for array
(3) Auto_ptrs is not a "Sihai universal" smart pointer
(4) Auto_ptrs does not meet the requirements of other elements of STL container
Two numerical limits (Numeric Limits)
1 C + + standard library provides extrema through template numeric_limits
The C-language integer constants are defined in the <climits> and <limits.h> floating-point constants defined in <cfolat> and folate.h
Minimum length of mid-build type:
Char 1byte
Short int 2byte
Int 2byte
Long int 4byte
Float 4byte
Double 8byte
Long Double 8byte
2 Numeric_limits not only provides a generic template but also provides its special version.
(1) Universal template, providing default extrema for all types
(2) Each specific type of extremum, provided by the special version
(3) template<>classnumeric_limits<int>
{
Static const BOOL Is_specialized=true;
Static T min () throw ()
{
Return-2147483648
}
..........
}
Set Is_specialized to true here all other members are set according to the specific type extremum
Examples of Use:
cout<<boolalpha<<endl;
cout<<"Max"<<numeric_limits< short >::max () << Endl
cout<<"Max (long)"<<numeric_limits<long>::max () <<endl;
cout<< "String is_specialized" <<numeric_limits<string>::is_specialized () <<endl;// Whether to define numerical limits
Three head files <cstddef> <cstdlib>
Various definitions within the <cstddef>
Null is also defined in <cstdio><cstdlib><cstring><ctime><cwchar><clocle>
size_t an unsigned type used to represent the size of a number of elements
ptrdiff_t a type with a sign to represent the distance of a pointer
Offsetof represents the offset of a member in a struct or union
Various definitions within the <cstdlib>
The arguments that exit_success and exit_failure use as exit () can also be used as the return value of main ()
Eixt (Intstatus) exit (Leave exit) program (and clean up static objects)
Abort () Exit program (may cause a crash on some systems)
Atexit (void (*) function ()) Call a function when exiting a program
Five C + + Standard Template Library
1 STL Build (STL components)
Key builds: containers, iterators, algorithms
The basic idea of STL is to separate the data from the operations, the data is managed by the container class, the operation is defined by the customizable algorithm, and the iterator acts as an adhesive between the two, so that any algorithm can interact with any container.
2 Containers (Containers) and iterators
Categories of iterators:
1 bidirectional iterators:
Can be done in both directions, forward with an increment operation, or back with a decrement operator (the list set Multiset map Multimap provides such iterators)
2 random time-of-storage iterators:
Random access iterators have all the attributes of a bidirectional iterator with random access: vector deque string
Differences between 3:
For (Pos=coll.begin ();p os!=coll.end (); ++pos)
{
.....................
}
For (Pos=coll.begin ();p os<coll.end (); ++pos)
.........
Only random-access iterators support operation<<; So the code is generally written in the first
Container-related functions:
Reverse () flips the interval energy element
Coll2.resize (Coll.size ());//Change the number of COLL2 elements
Deque<int>coll3 (Coll1.size ()); Specify enough space to initialize
Adapter for six iterators:
(i) Three types of iterator connectors:
1 Insert iterators (placement-type iterator)
2stream iterators (Stream iterator)
3Reverse iterators (Reverse iterator)
Placement-type iterators:
Inset iterators can solve the problem of "insufficient target space" of the algorithm by making the algorithm work in the form of placement rather than overwrite it. It will cause the target range to grow as needed.
If setting a value on an element raises the placement of the owning cluster, the insertion position at the top or last of the container, or at a specified location, depends on three cases:
(1) Stepping forward without causing any movement
1.1 Back Inserters (placed at the end of the container)
Back inseters internal Call push_back () inserts an element at the end of the container
Copy (Coll1.begin (), Coll1.end (), Back_inserter (COLL2)
(Only vector deque list provides push_back ())
2.2 Front Inserters (placed in the front of the container)
Copy (Coll1.begin (), Coll1.end (), Front_inserter (Coll3));
This action reverses the order of the inserted elements if you first insert 1 and then 2, then 1 will supply the Push_front container behind 2 only: Deque list
2.3 Generalinerters (General placement Device):
General inserters Its role is to insert an element into the initialization when it takes the position of the second argument in front of the inserters internal call member function insert () and make parameters with the new value and new position all STL containers provide the insert () function
(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 produces a streamiterator that can read data from the standard input stream cin
2.2 Istream_iterator<string> ()
Calling Istreamiterator's default constructor produces an iterator that represents the "flow end" symbol, which means you can't read anything from it.
2.3 ostream_iterator<string> (cout, "\ n")
Produces an output stream iterator, operator<< to cout by writing strings second argument as a separator between elements
(2) Reverse Iterator (reverse iterator)
Reverse mode of operation
All containers can produce reverse through the member functions Rbegin () and rend () iterator
(ii) easier-to-type algorithms (Manipulatingalgorithm to delete or rearrange or modify elements)
1 algorithm Remove () removes an element from an interval
for (inti=1;i<=6;++i)
{
Coll.push_front (i);
Coll.push_back (i);
}
Remove (Coll.begin (), Coll.end (), 3);
Results: 654211245656
A value of 3 is removed and then overwritten by subsequent elements, as for the cluster end that violates the overridden elements, but logically, those elements are no longer part of the cluster.
Improved:
List<int>::iterator End=remove (Coll.begin (), Coll.end (), 3)
Copy (Coll.begin () end,ostream_iterator<int> (cout,","));
Results: 6542112456
or by:
Distance (End,coll.end ());
Distance () is the distance between the two iterators returned
If you really want to remove the element extermination you must call the corresponding member function container of the container to provide the member function erase () for this purpose erase () can delete all elements within the "parameters indicated interval"
2 Easier-to-type algorithms and related containers
The Erase () member function returns the number of deleted elements
(iii) functions as parameters of the algorithm
void print (intelem)
{
cout<<elem<<"";
}
For_each (Coll.begin (), Coll.end (), print);
2 Judging formula (predicate)
The so-called predicate is a function that returns a Boolean value that they typically use to specify collations and search criteria, not any return bool worth a unary function or a two-tuple function that is legal predicate STL requires the same value to be faced predicate must produce the same result, This commandment excludes "functions that change their internal state when called."
3 functor (Functors,functionobject function object)
The "function parameter" passed to the algorithm is not necessarily a function, it can be an object that behaves like a function
The advantages of the affine function:
(1) Intelligent ambiguous smart point
An functor can have member functions and member variables which means that the functor has state, and secondly you can initialize them at the initial time, and of course must before they are called
(2) The functor has its own type (can design function inheritance system)
(3) Imitate function faster than general function
Pre-defined faux functions:
Set<int>coll; will be extended to set<int,less<int>>coll;
So the reverse arrangement of these elements can be set<int,greater<int>>coll;
Through a number of special function connectors you can combine predefined functor and other values or use special conditions
For example:
Transform (Coll.begin (), Coll.end (), Back_inserter (COLL2), bind2d (Mltiiplies<int> (), 10)
Multiply all the elements in the Coll by 10 and place them into the COLL2 where the adapter is used to make the multiple<int> operation take the element of the source cluster as the first parameter, and 10 as the second argument
An imitation function is generally declared as: inline
Special Imitation functions:
For_each (Coll.begin (), Coll.end (), Mem_fun_ref (&person::save ());
The functor Mem_fun_ref a member function used to invoke the element he is acting on.
(iv) elements within the container
1 Conditions for container elements
(1) You must be able to copy through the copy constructor, and the performance of the copy function is optimized as possible
(2) The assignment operation (i.e. = number) must be completed via the assignment operator
(3) You must be able to complete the destruction of an action destructor through a destructor. No, it can never be designed as Preivate
Destructors should never throw exceptions
(4) For a sequence container, the element's default constructor must be available
(5) For some actions you must define operatpor== to perform an equality test
(6) In an associative container, the element must define a collation by default operator< is called through the functor Less<>
2 value semantics and reference semantics
All containers make a copy of the element and return the copy. This means that the elements inside the container are equal to the objects you put in, but not the same
STL Standard Library First part summary (auto_ptr&&numeric_limits, Standard Template Library, iterator adapter)