Stash4.h
/*** book: "thinkinginc++" * Function: stash* time with inline function: September 12, 2014 08:16:13* Author: cutter_point*/#ifndef stash4_h_included#define Stash4_h_included#include ". /require.h "#include <iostream>class stash{int size; The byte length of each space storage block int quantity; The number of storage blocks for internally defined data types int next; Index of the next empty memory block unsigned char* storage; The size of the storage space void inflate (int increase); Increase space size when space is not enough public://constructor, use inline function to improve code execution efficiency Stash (SZ): size, Quantity (0), next (0), storage (0) {} Stash (int sz, int initquantity): Size (SZ), quantity (0), next (0), storage (0) {inflate (initquantity);} ~stash ()//Reclaim memory space {if (storage! = 0) Delete [] storage; std::cout<< "~stash () Reclaim memory Space" <<std::endl; } int Add (void* element); add element void* Fetch (int index) const {require (0 <= index, "stash::fetch (-) index"); if (index >= next) return 0; Size beyond the memory space Return & (Storage[index*size]); } int count () const {Return Next;}}; #endif//stash4_h_included
Stash4.cpp
/*** book: "thinkinginc++" * Function: stash* time with inline function: September 12, 2014 08:17:14* Author: cutter_point*/#include "Stash4.h" #include < Cassert> #include <iostream>using namespace std;const int increment=100;//void inflate (int increase); Increase space size when space is not enough//int Add (void* element); add element void stash::inflate (int increase)//space is not enough time to increase the space size {//Determine increase is greater than 0 of the assert (increase >= 0); If the judgment is equal to 0, if it equals 0 then return directly without adding if (Increase = = 0) return; More than 0 words//1, the total number of new space to obtain int newquantity=quantity+increase; 2, the new space requirements of the number of bytes int newbytes=newquantity*size; 3, the application of new space requirements of memory unsigned char* b=new unsigned char[newbytes]; 4, bar The old space data into a new space//first, as long as the old member data directly number int oldbytes=quantity*size; Move to the new on for (int i=0; i<oldbytes; ++i) b[i]=storage[i]; 5, memory recovery Delete [] (storage); 6, the new Space member data Update storage=b; quantity=newquantity;} int Add (void* element); Add an element and return the position of the last element int Stash::add (void* Element) {//judgment can not be added, space enough if (next >= quantity) infLate (increment); Find the start position to add int startbytes=next*size; The element is converted into an array of unsigned char* unsigned char* e= (unsigned char*) element; Add an element to the data type for (int i=0; i<size; ++i)//The byte length of an element is to add the length of an element storage[startbytes+i]=e[i]; Bar Next update ++next; Return to position return (NEXT-1);}
Stash4Test.cpp
/*** book: "thinkinginc++" * Function: stash* time with inline function: September 12, 2014 08:18:19* Author: cutter_point*/#include "Stash4.cpp" #include ". /require.h "#include <fstream> #include <iostream> #include <string>using namespace Std;int main () { Stash Intstash (sizeof (int)); is to convert the int type for (int i=0; i<100; ++i)//bar 0 to 99 in the array of this data type Intstash.add (&i); All output see for (int j=0; j<100; ++j) cout<< "Intstash.fetch (" <<j<< ") =" <<* (i nt*) Intstash.fetch (j) <<endl; Then create a string array of length 80, each string length is 80 bytes const int bufsize=80; Stash Stringstash (sizeof (char) *bufsize, 100); Ifstream in ("Stash4Test.cpp"); Assure (in, "Stash4Test.cpp"); String line; while (Getline (on, line)) {Stringstash.add ((char*) line.c_str ()); The string converted into char* into the array}//output to see int k=0; Count char* CP; Point to each string's address while ((cp= (char*) Stringstash.fetch (k++))! = 0) cout<< "Stringstash.fetch (" <<k<< ;") = "<<cp<<endl; return 0;}
Finally, let's take a look at the knowledge about enum types
Enum type directly outputs the characters inside
Http://topic.csdn.net/t/20050408/19/3921153.html
Scenario one, directly with the array
Scenario two, using map
Class Mptypeconverter {Public:mptypeconverter () {Map.insert (Make_pair (Mpt_none, "Mpt_none")); Map.insert (Make_pair (Mpt_other, "Mpt_other")); Map.insert (Make_pair (Mpt_board, "Mpt_board"));
"Thinkinginc++" 49, stash with inline functions