Processing object pointers in STL

Source: Internet
Author: User

The STL container class stores and Manages objects. Although there are no clear restrictions on the inclusion of pointers in STL container classes, STL does not care whether you store objects in containers or pointers. However, considering the original intention of STL, it is obviously out of date to use pointers in the container class. Instead of placing the object pointer in the STL container class, we should try our best to directly put the object itself. One of the biggest side effects of directly storing pointers in containers is that memory leakage may occur. This problem is highlighted in the char * type.

However, in some cases, direct use of object pointers has obvious advantages. These situations can be summarized as follows:

    1. When objects are large, the system resource consumption caused by frequent copy and replication is very expensive.
    2. It is not uncommon to store the same object in multiple containers.
    3. You need to store multiple derived class objects derived from the same parent class in the same container. This is also common.

In fact, the computing I developed this weekProgramIs facing the third situation. Consider the advantages of using STL. I decided to introduce the STL list container. Originally, using the tlist object of BCB can achieve the same purpose. However

    • When the number of object pointers in the tlsit class exceeds 5000, the efficiency will be significantly reduced.
    • The tlist class is not of type security and does not care about the type of the introduced object pointer.

Introducing the tlist class means to include the VCL. h header file, which is not a good thing for the portability of my computing modules.

After I made the decision, I faced two STL-related problems.

The first problem is how to process object pointers in STL. My solution is to create a class that encapsulates pointers,CodeAs follows:

// Define a pointer encapsulation class for STL containers <br/> // it is not appropriate to add a pointer directly to the container because STL is used. <Br/> //////////////////////////////////// //////////////////////////////////////// //// <br/> class ptrwrapper <br/>{< br/> PRIVATE: <br/> X * PX; // pointer to Class X </P> <p> Public: <br/> file: // constructor and copy constructor <br/> ptrwrapper (x * x = 0): Px (x) {}< br/> ptrwrapper (const ptrwrapper & PW ): px (PW. px) {}< br/> file: // destructor <br/> ~ Ptrwrapper () {}< br/> ptrwrapper & operator = (const ptrwrapper & PW) {PX = XW. px ;}</P> <p> file: // overload operator () returns the pointer to object x <br/> const x * operator ()() const {return PX ;}< br/> X * operator () {return PX ;}< br/>}; </P> <p> file: // reload logical operators ==,<,> <br/> bool operator = (const ptrwrapper & pw1, const ptrwrapper & pw2) {<br/> return (pw1.operator () () & pw2.operator ()())? * Pw1 () = * pw2 (): false; <br/>}</P> <p> bool operator <(const ptrwrapper & pw1, const ptrwrapper & pw2) {<br/> return (pw1 () & pw2 ())? * Pw1 () <* pw2 (): false; <br/>}</P> <p> bool operator> (const ptrwrapper & pw1, const ptrwrapper & pw2) {<br/> return (pw1 () & pw2 ())?! (* Pw1 () <* pw2 (): false; <br/>}< br/>

the above Code encapsulates a pointer. After ptrwrapper-like encapsulation, you do not need to use pointers directly. STL containers touch real objects, but this object encapsulates a specific type of pointer. The following example describes the use of ptrwrapper.

// Assume that you need to put the pointer to the object of Class X into the STL container. <Br/> // <br/> Class X <br/>{< br/> PRIVATE: <br/> int I; </P> <p> public: <br/> file: // constructor, copy constructor, destructor <br/> X (int I): I (I) {}< br/> X (const X & X): I (X. i) {}< br/> ~ X () {}< br/> file: // overload operator =, () <br/> X & operator = (const X & X) {I = x. I ;}</P> <p> int operator () const {return I ;}< br/>}; <br/> file: // reload the logical operators <br/> bool operator = (const X & X1, const X & x2) {<br/> return x1 () = x2 (); <br/>}</P> <p> bool operator <(const X & X1, const X & x2) {<br/> return x1 () <X2 (); <br/>}</P> <p> file: // The following is the sample main program. <br/> int main (INT, char * []) {<br/> ptrwrapper bucket [5]; <br/> for (INT I = 0; I <5; ++ I) {<br/> bucket [I] = ptrwrapper (New X (I * I); <br/>}< br/> random_shuffle (bucket, bucket + 5 ); </P> <p> List <ptrwrapper> list1; <br/> copy (bucket, bucket + 5, <br/> back_insert_iterator <list <ptrwrapper> (list1) <br/>); </P> <p> cout <"List of ptrwrapper: ("; <br/> for_each (list1.begin (), list1.end (), print); <br/> cout <")" <Endl; </P> <p> set <ptrwrapper, greater <ptrwrapper> set1; <br/> copy (list1.begin (), list1.end (), <br/> insert_iterator <set <ptrwrapper, greater <ptrwrapper> <br/> (set1, set1.begin () <br/>); </P> <p> cout <"set of ptrwrapper: ["; <br/> for_each (set1.begin (), set1.end (), print); <br/> cout <"]" <Endl; </P> <p> deque <ptrwrapper> deque1; <br/> copy (list1.begin (), list1.end (), <br/> back_insert_iterator <deque <ptrwrapper> (deque1) <br/> ); </P> <p> cout <"deque of ptrwrapper: ("; <br/> for_each (deque1.begin (), deque1.end (), print ); <br/> cout <")" <Endl; </P> <p> return 0; <br/>}

Related Article

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.