Processing object pointers in STL

zhaozj2021-02-08  223

Processing object pointers in STL

It is well known that STL's container class is aimed for storage management. Although there is no clear limit that cannot be included in the STL container class, STL does not care about the objects you store in the container or pointers. But carefully consider the original intention of STL, using a pointer in the container class is obviously not appropriate. CKER means that you should use the object itself directly instead of the object's pointer in the STL container class. A maximum side effects that store pointers directly in the container may result in memory leaks. This problem has a prominent manifestation in the char * type. But some time, the direct use of the object pointer has a significant benefit, which can generally summarize as a few aspects: 1. When the object is very large, the frequent copy replication is very expensive. 2. This situation should not be rare when you need to put the same object in multiple containers. 3. When you need to store multiple derived class objects derived from the same parent class in the same container. This is also very common. In fact, in the calculation program developed in CKER, it is necessary to face the third situation. Consider the benefits of using STL. CKER decides to introduce the STL LIST container.

Originally, the same purpose can be achieved using the TLIST object of BCB. However, the TLSIT class has more than 5,000 in the number of object pointers, and the efficiency will have a significant decline. The TLIST class is not type safe, and it does not care about the introduced object pointer. The introduction of the TLIST means that the vcl.h header file is to be included, which is really not a good thing for my calculation module.

After CKER did a decision, it faced two questions related to STL. The first question is how to process the pointer to the object in STL mentioned above. The solution of CKER is a class that creates a package pointer. code show as below:

// Define a pointer package class for STL containers // because use STL should not be directly added to the container directly. Class PTRWrapper {private: x * px; // Pointer to class X

Public: file: // Constructor PTWrapper (x * x = 0): PX (x) {} Ptrwrapper (Const PTRWrapper & Pw): PX (PW.PX) {} file: // Destructor ~ PTRWrapper ()} Ptrwrapper & operator = (const ptrwrapper & pw) {px = xiw.px;}

File: // Overload operator () Returns the pointer const x * Operator () const {return;} x * operator () ()} x * operator ()};

File: // Overload logic operator ==, <,> BOOL OPERATOR == (Const PTRWrapper & PW2) {Return (pw1.operator () () && pw2.operator () ())? * PW1 () == * pw2 (): false;}

Bool Operator <(Const PTRWrapper & PW2) {RETURN (PW1 () && pw2 ())? * pw1 () <* pw2 (): false;}

Bool Operator> (Const PTRWrapper & PW2) {RETURN (PW1 () && pw2 ())?! (* pw1 () <* pw2 ()): false;}

The above code encapsulates a pointer. After the package of PTRWrapper, you don't have to use the pointer directly. The STL container is in contact with the true object, but this object encapsulates a specific type of pointer. The following is an example of the use of PTRWrapper. / / Suppose you need to put a pointer to the object to the class X in the STL container. // Class X {Private: INT I;

Public: file: // Constructor, copy constructor, destructor X (INT I): i (i) {} x (const x): i (xi) {} ~ x () {} file: / / Overload operator =, () x & operator = (const X) {i = xi;}

INT Operator () () const {return i;}}; file: // overload logic operator BOOL OPERATOR == (Const X & X1, Const X & X2) {Return X1 () == x2 ();}

Bool Operator <(Const X & X1, Const X & X2) {RETURN X1 ()

File: // The following is an example primary program int main (int, char * []) ​​{ptrwrapper bucket [5]; for (int i = 0; i <5; i) {bucket [i] = ptrwrapper (new X (i * i));} random_shuffle (bucket, bucket 5);

List1; Copy (BUCKET, BUCKET 5, Back_INSERT_ITERATOR > (List1));

Cout << "List of ptrwrapper: ("; for_each (list1.begin (), list1.end (), print); cout << ") <<< ENDL;

Set > set1; copy (list1.begin (), list1.end (), insert_iterator >> (set1, set1.begin ())));

COUT << "set of ptrwrapper: ["; for_each (set1.begin (), set1.end (), print); cout << "]" << ENDL;

Deque deque1; copy (list1.begin (), list1.end (), back_insert_iterator > (deve1));

Cout << "Deque of Ptrwrapper: ("; for_each (deque1.begin (), demin1.end (), print); cout << ") << ENDL;

Return 0;}

There is also a problem with how to store the genus object in STL container, next discussion. Note: The code prototype in the article comes from the Internet, but unfortunately the URL has not been left. After the CKer was organized, it was posted, and it was called the original.

You can copy, distribute, download this document free. However, you may not take it, change this article, or use this article to see any form of interest. If you have any comments and suggestions, please allow mailto: cker@sina.com

转载请注明原文地址:https://www.9cbs.com/read-2231.html

New Post(0)