STL overview-common tools pair, auto_ptr, STD: rel_ops

Source: Internet
Author: User

Common tools in the C ++ standard library are mostly short and refined functions or classes to complete the most common tasks, which are roughly as follows:

 

========================================================== ================================

Peer Group

|

|-> Name -----> pair

|-> Motivation -----> convenience

|-> Idea -----> Structure

|-> Application -----> it may be useful if a function requires two values to be returned.

|

|-> Type -----> struct

|-> Include ---> <utility>

|-> Define ----> pair <calss first, calss second> (first, second)

|

|-> Member

| ------> First

| ------> Second

|

|-> Sub

| ------> Constructor (default, assignment, copy)

|

|-> Fun

| ------> Operator (==,<,<=, >,>= ,! =, =)

| ------> Make_pair (first, second) returns a new pair

 

 

========================================================== ================================

Smart pointer

|

|-> Name -----> auto_ptr

|-> Motivation -----> prevent "resource leakage occurs when an exception is thrown"

|-> Idea -----> use a class (auto_ptr) to include new and delete

|-> Application -----> if you want to use new, think of it as much as possible

|-> Personality

| ------> ① Lives together with the object mentioned -- Core

| ------> ② Because ①: One thing (object) and two masters (auto_ptr) are strictly prohibited)

| ------> ③ Because ②: Ownership Transfer-this is the most interesting, but dangerous.

| ------> ④ The pointer has an unequal position.

|

|-> Trap

| ------> ① Initialize two auto_ptr objects with the same object

| ----> Violation of personality ②

| ------> ② There is no overload assignment operator in the class

| ----> A problem may occur due to personality ③, similar to copy constructor

| ------> ③ Define the auto_ptr Array

| ----> Irrelevant to the core stated in ①

|

|-> Type -----> class

|-> Include ---> <memory>

|-> Define ----> auto_ptr <class> (New Class)

|

|-> Sub

| ------> Constructor (default, assignment, copy)

| ------> Reset (PTR) reinitializes auto_ptr with PTR

|

|-> Fun

| ------> Operator (*,->, =)

| ------> Get () returns the address of the object indicated by auto_ptr and does not release its own ownership.

| ------> Release () returns the address of the object indicated by auto_ptr, but releases its own ownership.

 

 

========================================================== ================================

Three auxiliary functions

|

|-> Name

| -----> Max <class> (x, y), max <class> (X, Y, compare)

| -----> Min <class> (x, y), Min <class> (X, Y, compare)

| -----> Swap <class> (x, y)

|

|-> Motivation -----> convenience

|-> Idea -----> Function

|-> Application -----> too many

|

|-> Description

| ------> ① Both Max and Min have two parameter forms. The first one is good to say, and the second conpare represents the comparison function.

| ------> ② The premise that swap can be correctly executed is that your <class> must have copy costructor and assignment

| This is determined by swap's internal mechanism-it calls copy to generate an intermediate object and then calls it | assignment rotates the object

|

|-> Type -----> Function

|-> Include ---> <algorithm>

 

 

========================================================== ======================================

A concise form of comparison Operators

|

|-> Name -----> STD: rel_ops

|-> Motivation -----> reuse code as much as possible

|-> Idea -----> because the comparison operation has only two basic values: <and = (of course, it can also be> and =, but STL selects the previous one ). | Other operations can quickly produce results based on these two script operations. Then, of course, we can encapsulate other comparison operations in some form and use them directly. STL adopts the namespace approach-STD: rel_ops | included> ,! =, <=, >= These four comparison operations.

|

|-> Application -----> If the class you write needs to overload the comparison operator, a simple sentence of using namespace STD: rel_ops is | it's a simple task. I believe that every lazy person like me will like it.

|

|-> Trap -----> STD: rel_ops is just for you> ,! =, <=,> = These four comparison operations are encapsulated, and their implementation is built | based on your own correct heavy load <and =. Don't expect nothing to do. One thing | STD: rel_ops will be able to do everything well. How can it be so beautiful ?!

|

|-> Type ----> namespace

 

 

The above is defined by the C ++ Standards Committee, and does not mean that all compilers follow this principle. (For example, the compiler I use now does not have STD :: tel_ops), learning the idea. If you want to, you can implement it yourself under the guidance of such ideas. This is fun.

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.