C + + generic tools-from C + + standard Library

Source: Internet
Author: User

Learn the C + + standard library tonight to briefly review and summarize.

  1.pair Structural Body

//defined in <utility>, the Std namespacenamespacestd{Template<classT1,classT2>structpair{//type names for the valuestypedef T1 First_type;        typedef T2 SECOND_TYPE; //memberT1 first;        T2 second; //default constructor//implicit invoke the built-in types to provide a default valuepair (): First (T1 ()), second (T2 ()) {}//constructor for both default valuesPairConstt1& A,Constt2&b): First (a), second (b) {}//copy constructor with implicit conversions, templates are used because implicit type conversions may be required during construction. template<classUclassV>pair (Constpair<u,v>&p): First (P.first), second (P.second) {}}; //comparisionsTemplate <classT1,classT2>BOOL operator== (ConstPair<t1,t2>&,Constpair<t1,t2>&); Template<classT1,classT2>BOOL operator< (ConstPair<t1,t2>&,Constpair<t1,t2>&); //similar:! = <, =, >Template <classT1,classT2>pair<T1,T2> Make_pair (ConstT1&,Constt2&);}

2. Make_pair (): Allows you to generate a pair object without having to write the type.

 namespace   std{ // create value pair only by providing the values.  Template <class  t1, pair  <T1,T2> Make_pair (const  t1& x,constt2& Y) { return  pair<t1,t2> (x, y); }}

You can use std::make_pair("@") , rather than write: std::make_pair<int ,char> ("@");

3. Smart pointer auto_ptr

  Why use smart pointers?

If the resource obtained at the beginning is bound to a local object, the destructor of the object is called when the function exits, freeing the resource. There are 2 common problems:

1. Often forget the delete operation

2. When a function error is returned, the delete may not have been executed

You can use catch all exceptions to resolve and then perform a delete operation at the end. But more cumbersome! is not a good appearance style, and complex error-prone.

  the role of smart pointers: to ensure that, in any case, as long as they are destroyed, it is necessary to release the resources they refer to; Because the smart pointer itself is a local variable, either normal exit or abnormal exit, as long as the function exits, the local variable will be destroyed. This also shows that the local object is destroyed, but its bound resources are still not released.

  smart pointer definition: auto_ptr is a pointer to the object that is pointing to the owner! And is the only owner, an object can only correspond to one smart pointer.

  Smart pointers are defined in header files <memory>

  Smart Pointer Declaration:  std::auto_ptr<classa> ptr (new ClassA); You do not need to use catch and delete after you have used auto_ptr.

  access to smart pointers: similar to general pointers, with *,-and and = operations, but does not allow direct assignment of normal pointers to Auto_ptr

  Ownership relationship of Smart pointers:

+ = transfer: Through the Auto_ptr copy constructor to transfer ownership. This allows the assignment operation to change the source variable, violating the STL's requirements for the container element, so auto_ptr is not allowed as a container element of the STL.

Std::auto_ptr<classa> ptr1 (new  ClassA) std::auto_ptr<ClassA> ptr2 (PTR1);  // if Ptr2 binded another obj, it'll be deleted before. Then PTR1 is a null pointer!

  usage of auto_ptr: (derived from the special usage of ownership transfer)

(1) A function is the starting point of a auto_ptr: the ownership must be passed out, otherwise it will be deleted when the function exits, here is the starting point of the data.

(2) A function is the end point of the auto_ptr: if you do not need to use auto_ptr, you do not have to pass it out, it will be automatically deleted.

  Above is the value of the Auto_ptr pass, if it is a reference pass and a const type?

  by Reference: When passing through reference, it is not possible to know whether ownership is being transferred, so the design of this method is not recommended.

  by Const: cannot change control (but can modify object properties), security is enhanced, many interfaces in STL require internal copy all through const reference.

  The use of AUTO_PTR requires attention:

(1) Ownership cannot be shared between Auto_ptr objects.

(2) Auto_ptr is not designed for array, because delete is used instead of delete[].

(3) Auto_ptr objects are destroyed only when the smart pointer that owns the object is destroyed.

(4) Auto_ptr does not meet the requirements of STL container elements.

C + + generic tools-from C + + standard Library

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.