Use of "thinkinginc++" 66, pointer stash

Source: Internet
Author: User

Header file PStash.h


/*** book: "thinkinginc++" * Function: pointer stash's header file * Date: October 5, 2014 14:33:15* Author: cutter_point*/#ifndef pstash_h_included# Define Pstash_h_includedclass pstash{    int quantity;   The number of storage blocks for internally defined data types    int next;       The position of the next empty space    void** storage;     Point to a pointer to void*    void inflate (int increase);//Increase memory space public:    //Constructor    Pstash (): quantity (0), storage (0), Next (0) {}    ~pstash ();  destructor    int Add (void* element);//add element    void* operator [] (int index) const;    Operator overload    void* Remove (int index);        Remove the element under the index index    int count () const {return next;}    Returns the total number of elements}; #endif//pstash_h_included

Definition file PStash.cpp

/*** book: "thinkinginc++" * function: pointer stash definition file * Time: October 5, 2014 14:33:49* Author: cutter_point*/#include "PStash.h" #include " ..   /require.h "#include <iostream> #include <cstring>using namespace std;/* int quantity;       The number of storage blocks for internally defined data types int next;     The position of the next empty space void** storage; Point to a pointer to void* void inflate (int increase);  Add memory Space public://Constructor Pstash (): quantity (0), storage (0), next (0) {} ~pstash (); destructor int Add (void* element);    add element void* operator [] (int index) const;        Operator overload void* Remove (int index);    Remove the element under the index index int count () const {return next;}    Returns the total number of elements */void pstash::inflate (int increase)//increased memory space {const int psz=sizeof (void*); Find the length of each minimum storage unit void** st=new Void*[quantity+increase];    Increased space//bar New Space Initialization memset (ST, 0, (quantity+increase) *psz);    The contents of the old space are copied to the new space memcpy (ST, storage, QUANTITY*PSZ);    Bar data Refresh Quantity+=increase;    Reclaim the corresponding space delete []storage; Refresh Data storage=st;} ~pstash ();    destructor Pstash::~pstash () {for (int i=0; i<next; ++i) require (storage[i] = = 0, "Pstash not cleaned up"); delete []storage;} int Add (void* element);   add element int Pstash::add (void* Element) {//Add element//To determine if a given space is enough, not enough that increases the const int inflatesize=10;    Used to increase the length if (next >= quantity) inflate (inflatesize);    The space is enough, then the element input into the array inside to storage[next++]=element;    return (NEXT-1);    Bar The added index returns}//void* operator [] (int index) const; Operator overloading void* Pstash::operator [] (int index) const{//Whether the index to be tested is reasonable require (index >= 0, "Pstash::operator [] Ind    ex negative ");    Since the data is reasonable, determine if the data exceeds the bounds if (index >= next) return 0; Returns the data for the corresponding index return STORAGE[INDEX];}        void* Remove (int index);    Remove the element under Index index void* pstash::remove (int index) {void* v=operator[] (index);   Remove the pointer if (v! = 0) storage[index]=0; Here, after the pointer is set to 0, but there is no change in the location of the memory, the next added memory will start or next return v;}

The final test file PStashTest.cpp


/*** book: "thinkinginc++" * Function: pointer stash test file * Time: October 5, 2014 14:34:23* Author: cutter_point*/#include "PStash.cpp" # Include ".    /require.h "#include <iostream> #include <fstream> #include <string>using namespace Std;int main () {    Pstash Intstash;    for (int i=0; i<25; ++i) intstash.add (new int (i)); Output element content for (int i=0; I<intstash.count (); ++i) {cout<< "intstash[" <<i<< "] =" <<*    (int*) intstash[i]<<endl;    }//clear, Reclaim memory for (int i=0; I<intstash.count (); ++i) Delete Intstash.remove (i);    Output the current file Ifstream in ("PStashTest.cpp");    Assure (in, "PStashTest.cpp");    Pstash Stringstash;    String line;    while (Getline ()) {Stringstash.add (line)); }//output string for (int u=0; stringstash[u]; ++u) cout<< "stringstash[" <<u<< "] =" <<* (strin    g*) stringstash[u]<<endl; Clear memory for (int v=0; V<stringstash.count (); ++v) Delete (String*) Stringstash.remove (v); return 0;}






Use of "thinkinginc++" 66, pointer stash

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.