PRODUCT : Borland C++ NUMBER : 1739 VERSION : All OS : All DATE : November 2, 1993 PAGE : 1/2 TITLE : Using a dictionary container to store strings The ClassLib manual is sparse on examples. This is one of many TI's that supplies examples for the ClassLib. This example illustrates the use of the Dictionary by inserting String objects. If you wish to include your own objects TI1381 explains the process necessary. A dictionary is a collection of Association objects. In fact if you try to add a non-Association type object you will receive a runtime error. The example below is very simple and should be to follow. When compiling the following program, be sure to link in the container class library. If you're using the IDE, have Options | Linker | Libraries | Container class library | Static checked. If you're using the command-line compiler, add the appropriate tclass.lib, depending on memory model. #include // used for the String objects #include #include // header for Association type #include // header for Dictionary type void main() { // Create a Dictionary instance. Dictionary dict; // Create strings to use as data for Dictionary. String& cool1 = *new String("Dexter Gordon"); String& inst1 = *new String("Tenor"); // Next create an Association of the two strings. // The first value is the key and the second is the value. // In this example the persons name is the key and their // instrument is the value. Association& entry1 = *new Association(cool1, inst1); // Add the association to the dictionary. dict.add(entry1); // Repeat the process for each dictionary entry. String& cool2 = *new String("Sonny Rollins"); String& inst2 = *new String("Tenor"); Association& entry2 = *new Association(cool2, inst2); dict.add(entry2); PRODUCT : Borland C++ NUMBER : 1739 VERSION : All OS : All DATE : November 2, 1993 PAGE : 2/2 TITLE : Using a dictionary container to store strings String& cool3 = *new String("Bill Evans"); String& inst3 = *new String("Piano"); Association& entry3 = *new Association(cool3, inst3); dict.add(entry3); String& cool4 = *new String("Charlie Parker"); String& inst4 = *new String("Alto"); Association& entry4 = *new Association(cool4, inst4); dict.add(entry4); String& cool5 = *new String("Wayne Shorter"); String& inst5 = *new String("Tenor"); Association& entry5 = *new Association(cool5, inst5); dict.add(entry5); String& cool6 = *new String("Clifford Brown"); String& inst6 = *new String("Trumpet"); Association& entry6 = *new Association(cool6, inst6); dict.add(entry6); // to print the dictionary just stream it out. cout << "\nJazz Masters\n" << dict; // Create a string containing the key to search for. String search4("Bill Evans"); // Next pass the string to the dictionary lookup() function. // The value() at the end is from Association and returns the // value of the object of the association. String& instrument = (String&) dict.lookup(search4).value(); cout << "\nMaster Bill Evans played " << instrument << endl; } DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.