#include #include #include #include #define FALSE 0 #define TRUE !FALSE using namespace std; template < class TKey, class TData > class dict { TKey *keys; TData *data; unsigned long int keysLength; unsigned long int dataLength; unsigned long int itemCount; public: TData& operator[]( TKey key ); dict(); ~dict(); }; template < class TKey, class TData > TData& dict< TKey, TData >::operator[]( TKey key ) { unsigned long int i; for ( i = 0; i < this->itemCount; i++ ) { if ( key == this->keys[i] ) return this->data[i]; } if ( this->itemCount == this->keysLength ) { this->keys = (TKey*) realloc( this->keys, ( this->itemCount + 10 ) * sizeof( TKey ) ); this->data = (TData*) realloc( this->data, ( this->itemCount + 10 ) * sizeof( TData ) ); this->keysLength += 10; this->dataLength += 10; } this->itemCount++; // Both of these give seg faults this->keys[ this->itemCount - 1 ] = key; //this->keys[0] = "Hello"; return this->data[ this->itemCount - 1 ]; } template < class TKey, class TData > dict< TKey, TData >::dict( ) { this->keys = (TKey*) malloc( 1 * sizeof( TKey ) ); this->data = (TData*) malloc( 1 * sizeof( TData ) ); this->keysLength = 1; this->dataLength = 1; this->itemCount = 0; } template < class TKey, class TData > dict< TKey, TData >::~dict( ) { free( this->keys ); free( this->data ); } int main ( int argv, char *argc[] ) { dict< string, int > dict1; dict1["12321"] = 12; return 0; }