"Thinkinginc++" 23, a Pocket C library

Source: Internet
Author: User



/*** function: A pocket C library * Time: August 17, 2014 08:05:26* Author: cutter_point*///This header resembles a C library #ifndef clib_h_included#define Clib_h_ includedtypedef struct cstashtag{    int size;       Size of each small space    int quantity;   Used to indicate how many bytes to allocate, the total space size    int next;       The number of bytes already stored in the data    //First, we do not know what type size to allocate, so use the smallest unit as the space size    unsigned char* storage;} Cstash;void Initialize (cstash* s, int size);   Initialize space size void Cleanup (cstash* s);    Reclaim memory int Add (cstash* s, const void* element);    add element void* Fetch (cstash* s, int index), int count (cstash* s), void inflate (cstash* s, int increase), #endif//Clib_h_includ ED


/*** function: A pocket C library, the definition of implementation * Time: August 17, 2014 08:05:55* Author: cutter_point*/#include "CLib.h" #include <iostream> #include    <cassert>using namespace Std;const int Increment=100;void Initialize (cstash* s, int sz) {s->size=sz;    s->quantity=0;    s->storage=0; s->next=0;}        int Add (cstash* s, const void* Element)//void unknown type {if (s->next >= s->quantity)//requires the next empty space to be larger than the type of space, then expand    Inflate (s, increment); Copy elements into storage//start int startbytes=s->next*s->size; in the next new space    Know where to start is the space without data unsigned char* e= (unsigned char*) element;    for (int i=0; i < s->size; ++i) s->storage[startbytes+i]=e[i];    s->next++;     return (S-&GT;NEXT-1);   Position index}void* Fetch (cstash* s, int index)//{assert (0<=index);    The index position should be no less than 0 if (index >= s->next) return 0; Return & (S->storage[index*s->size]);} int count (cstash* s) {return s->next;} void Inflate (cstash* s, int increase)//increase memory space {assert (Increase> 0);   Need to increase the amount of space to be greater than 0 int newquantity=s->quantity+increase;       New size of space int newbytes=newquantity*s->size;    The new number of bytes, this can get the memory space size int oldbytes=s->quantity*s->size;    unsigned char* b=new unsigned char[newbytes]; for (int i=0; i<oldbytes; ++i) b[i]=s->storage[i]; Copy the original data to the new memory area Delete [] (s->storage);   Reclaim the old space s->storage=b; The head pointer of the new space is assigned to S->storage S->quantity=newquantity;}        void Cleanup (cstash* s) {if (s->storage! = 0) {cout<< "Freeing storage" <<endl;    Delete [] s->storage; }}



/*** function: A pocket C library, the definition of implementation, use * Time: August 17, 2014 08:06:38* Author: cutter_point*/#include "CLib.h" #include "CLib.cpp" #include <    fstream> #include <iostream> #include <string> #include <cassert>using namespace Std;int main () {    Cstash Intstash, Stringstash;    int i;    char* CP;    Ifstream in;    String line;    const int bufsize=80;    Initialize (&intstash, sizeof (int));    for (i=0; i<100; ++i) Add (&intstash, &i); for (i=0; I<count (&intstash); ++i) cout<< "Fetch (&intstash," <<i<< ") =" <<* (int*) F    Etch (&intstash, i) <<endl;    cout<< "---------------------------------------" <<endl;    Initialize (&stringstash, sizeof (char) *bufsize);    In.open ("CLibTest.cpp");    ASSERT (in);    while (Getline ()) Add (&stringstash, Line.c_str ());    C_str () is a pointer to the line character i=0; while ((cp= (char*) Fetch (&stringstash, i++))! = 0) cout<< "Fetch (&intstash," <<i<< ") =" <<cp<<endl;    Cleanup (&intstash);    Cleanup (&stringstash); return 0;}




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.