Dictionary - a C++ template class to emulate VBScript's "Scripting.Dictionary" collection
In the examples below, the left side are taken directly from Microsoft published documentation for the Scripting.Dictionary ActiveX. The right side is the equivalent C++ code using the Dictionary<> template. As you can see, the syntax is nearly identical, with the only significant different is that the data types of the dictionary must to predefined, and the C++ methods require parentheses where the VBScript methods don't.
| 'VBScript sample code from Add Method Dim d Set d = CreateObject("Scripting.Dictionary") d.Add "a", "Athens" d.Add "b", "Belgrade" d.Add "c", "Cairo" |
Dictionary<string,
string> d; |
| VBScript sample code from both Count
property & Keys method dim a a = d.Keys For i = 0 to d.Count -1 s = s & a(i) & "<BR>" Next |
vector<string> a; string s; a = d.Keys(); for (int i = 0; i < d.Count(); i++) s = s + a[i] + '\n'; |
| VBScript sample code from Items method dim a a = d.Items For i = 0 to d.Count -1 s = s & a(i) & "<BR>" Next |
vector<string> a = d.Items(); for (i = 0; i < d.Count(); i++) cout << a[i] << endl; |
| VBScript sample code from Exists method If d.Exists("c") Then msg = "Specified key exists." Else msg = "Specified key doesn't exist." End If |
if (d.Exists("c")) cout << "Key: 'c' Exists" << endl; else cout << "Key: 'c' does not exist" << endl; |
| VBScript sample code from Remove method (docs never explain the return value of Remove method) a = d.Remove("b") 'Remove second pair |
d.Remove("b"); |
| VBScript sample code from Key property d.Key("c") = "d" 'Set key for "c" to "d". |
d.Key("c") = "d"; |
| VBScript sample code from RemoveAll
method (docs never explain the return value of RemoveAll method) a = d.RemoveAll 'Clear the dictionary |
d.RemoveAll(); |
Summary of Public Interface:
// Dictionary.h: interface/implementation for the Dictionary class. // // Dictionary is a templatized class which is a direct copy of // the VBScript Scripting.Dictionary object. All methods // and properties are provided with a syntax that is either // identical to the VBS implementation, or with only trivial // differences (mainly, C++ member functions require parentheses // where VBScript methods don't) // // Copyright 1998, James M. Curran, All Rights reserved. // May be used freely, provided copyright noticed remains. // May not be sold in source code form. // // email: JamesCurran@Mvps.org // WWW: http://www.NJTheater.Com/JamesCurran //////////////////////////////////////////////////////////////////////
template<typename KEY, typename ITEM>
class Dictionary : std::map<KEY, ITEM>
{
public:
Dictionary() {};
virtual ~Dictionary(){};
void Add(KEY k, ITEM i);
bool Exists(KEY k) const;
size_type Count() const;
void Remove(KEY k);
void RemoveAll();
std::vector<KEY> Keys() const;
std::vector<ITEM> Items() const;
DKH Key(KEY k);
DIH Item(KEY k);
DIH operator[](KEY k);
DIH operator()(KEY k);
friend std::ostream& operator<<(std::ostream& os, const DIH& dih);
};
Note: Anything above the returns a DIH or DKH can appear on either the right or left side of an assignment, (although Key() is a bit pointless on the right hand side). eg:
dict.Item("a") = "Apple";
dict("a") = "Apple";
dict["a"] = "Apple";
are all legal (and equivalent).
Demo is included with header file. Just compile & link a source file with the following three lines:
#define DICTIONARY_TEST
#include "dictionary.h"
void main() { dictionary_test(); }

Comments
Little Problem... Help!
Posted by Legacy on 05/11/2003 12:00amOriginally posted by: Jorge
Hello, this class is great!
I had a project assigned and I wrote down all the code in VB and then just used this library to be able to translate it.
But I have a problem... The problem is that I made a class that has a dictionary inside... And then I made a dictionary of that class. All works fine but I cannot do something like this:
class MyClass{
public:
Dictionary<string, string> MyDict;
};
Dictionary<string, MyClass> DictionaryOfMyClass
void main(){
MyClass Something;
Something = DictionaryOfMyClass.Item("Key");
}
It says that my class does not support the "=" operator. Why is this? I think it also happens if you declare a dictionary and you try to equal it to another dictionary:
Dictionary <string, string> d1;
d1.Add("key", "item");
Dictionary <string, string> d2;
d2 = d1;
But when you have no Dictionary inside of the class it will let you do something like:
class MyClass{
public:
std::vector<string> MyVector;
};
Dictionary<string, MyClass> DictionaryOfMyClass
void main(){
MyClass Something;
Something = DictionaryOfMyClass.Item("Key");
}
In this case this will work perfectly... But is there a way to make this work on the previous examples? Please help me... I have to finish it for tuesday and I am desperate.
Thanks... :)
ReplyThis is not necessary!
Posted by Legacy on 10/19/2002 12:00amOriginally posted by: Wilson
This is not necessary! This is just a rehash of standard map and MFC CMap serial of classes. This one does not support the following: Runtime Classes, Dynamic Creation, Object Diagnostics and Serialization (Persistence). It is better to use CMap...
ReplyFinally! A good database object exists!
Posted by Legacy on 04/09/2001 12:00amOriginally posted by: Darryl Agostinelli
I'm glad someone has taken the time to post a nice database object like this one.
Thanks!
Replydictionary file
Posted by Legacy on 02/09/2001 12:00amOriginally posted by: JB
maybe you can help me. i need a dictionary file for my program. i will be interacting with junior high students to help them with their sentence formation and i don't want to create an entire dictionary. i know there has to be a file out there somewhere. if you are aware of it please responsd to dcandman@aol.com and place DICTIONARY in the subject bar so i don't delete it (mistaking it for junk mail).
Thanx...
JB
ReplyVery good job man!
Posted by Legacy on 12/25/1999 12:00amOriginally posted by: Matro
Hey, really thank you! I was looking for a fast-to-use associative array and here it is, with style! :-))
ReplyC++ source code?
Posted by Legacy on 05/16/1999 12:00amOriginally posted by: Edward Chen
Reply