Dictionary is a C++ template designed to duplicate the functionality, and, for
the most part, the syntax of the Scripting.Dictionary ActiveX object built into VBA and
VBScript. It derives from an STL map<> template, so any of the STL algorithms
will also work with it. All of it’s member functions are inlined, so there’s only a
Dictionary.h file. There is no Dictionary.cpp.
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, |
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(); }