tdictionary is used by Delphi, C++builder is too laborious to use.
C + + is still usedstd::map代替。
void __fastcall tform2::formcreate (tobject *Sender) { std::mapnew std::map<string , string>; void __fastcall tform2::formcreate (tobject *Sender) { std::map<string, string> Dir ; }
Copy the following code in your Delphiunit.pas file:
UnitDelphiunit;Interface usesSystem.Generics.Collections;typetcity=classcountry:string; latitude:double; longitude:double; End; //You cannot use tdictionary<string, tcity> in C + + unless you first //instantiate it in Delphi as follows:Citydictionary =class(Tdictionary<string, tcity>) End; Implementation End.
View Code
Then right-click your Delphipackage project on the project Manager and select Build.
ADD the generated Delphi package library file to your C + + console application:
- Right-click your Cppapplication project on the project Manager and select Add.
- On the dialog box that opens, select the DelphiPackage.lib file, the previously built and click OK. You could find this file inside "C:\USERS\PUBLIC\DOCUMENTS\STUDIO\14.0\DCP".
-
Note: If You cannot see the expected file, ensure the file filter in the dialog box was "Any file (*. *)".
Copy the following code in your main.cpp file:
#pragmaHdrstop#pragmaArgsused#ifdef _win32#include<tchar.h>#elsetypedefChar_tchar; #define_tmain Main#endif#include<iostream>#include<DelphiUnit.hpp>Const DoubleEPSILON =0.0000001; int_tmain (intARGC, _tchar*argv[]) { //Create the dictionary.tdictionary__2<unicodestring, tcity*>* dictionary =NewTdictionary__2<unicodestring, Tcity*> (0); Tcity*City ; //ADD Some key-value pairs to the dictionary. City=Newtcity; City->country ="Romania"; City->latitude =47.16; City->longitude =27.58; Dictionary->add ("Iasi", city); City=Newtcity; City->country ="Kingdom"; City->latitude =51.5; City->longitude =-0.17; Dictionary->add ("London", city); City=Newtcity; City->country ="Argentina"; City->latitude =0;//wrong coordinatesCity->longitude =0;//On purpose.Dictionary->add ("Buenos Aires", city); //Display the current number of Key-value entries.Std::wcout <<"Number of pairs in the dictionary:"<< Dictionary->count <<"."<<Std::endl; //Try looking up "Iasi". if(Dictionary->trygetvalue ("Iasi", city)) Std::wcout<<"Iasi is located in"<< city->Country<<"with latitude ="<< city->Latitude<<"and longitude ="<< city->Longitude<<"."<<Std::endl; ElseStd::wcout<<"Could not find Iasi in the dictionary."<<Std::endl; //Remove the "Iasi" key from the dictionary.Dictionary->remove ("Iasi"); //Make sure, the capacity of the dictionary is set to the number of entries.Dictionary->trimexcess (); //Test If "Iasi" is a key in the dictionary. if(Dictionary->containskey ("Iasi")) Std::wcout<<"the key \ "Iasi\" is in the dictionary."<<Std::endl; ElseStd::wcout<<"the key \ "Iasi\" is not in the dictionary."<<Std::endl; if(Dictionary->containskey ("London")) { //Test How (with Kingdom, 51.5, -0.17) is a value in the dictionary ...Dictionary->trygetvalue ("London", city); if(City->country = ="Kingdom"&& (City->latitude-51.5) < EPSILON && (City->longitude--0.17) <EPSILON) Std::wcout<<"The value (kingdom, 51.5, -0.17) is in the dictionary."<<Std::endl; ElseStd::wcout<<"error:the Value (Kingdom, 51.5, -0.17) is not in the dictionary."<<Std::endl; //... but Containsvalue returns False if passed a different instance of//tcity with the same data, as different instances has different references.City =Newtcity; City->country ="Kingdom"; City->latitude =51.5; City->longitude =-0.17; if(dictionary->Containsvalue (city)) Std::wcout<<"error:a new instance of Tcity with values (kingdom, 51.5, -0.17) matches a existing instance in the Dictionar Y."<<Std::endl; ElseStd::wcout<<"A new instance of tcity with values (from Kingdom, 51.5, -0.17) does not match any existing instance in the Dictiona Ry."<<Std::endl; Delete City; } Else{std::wcout<<"error:the key \ "London\" is not in the dictionary."<<Std::endl; } //Update the coordinates to the correct ones.City =Newtcity; City->country ="Argentina"; City->latitude =-34.6; City->longitude =-58.45; Dictionary->addorsetvalue ("Buenos Aires", city); //Generate The exception "duplicates not allowed". Try{Dictionary->add ("Buenos Aires", city); } Catch(Exception &e) {std::wcout<<"Could not add entry. Duplicates is not allowed."<<Std::endl; } //Display all countries.Std::wcout <<"All countries:"<<Std::endl; Dynamicarray<TCity*> cityvalues = dictionary->values->ToArray (); Const intCitycount =cityvalues.get_length (); for(inti =0; i < Citycount; i++) {Std::wcout<< Cityvalues[i]->country <<Std::endl; } //iterate through all keys in the dictionary and display their coordinates.Std::wcout <<"All cities and their coordinates:"<<Std::endl; Dynamicarray<UnicodeString> Citykeys = dictionary->keys->ToArray (); for(inti =0; i < Citycount; i++) {Dictionary-TryGetValue (Citykeys[i], city); Std::wcout<< Citykeys[i] <<": "<< City->latitude <<", "<< City->longitude <<Std::endl; } //Clear all entries in the dictionary.Dictionary->Clear (); //There should is no entries at the this point.Std::wcout <<"Number of Key-value pairs in the dictionary after cleaning:"<< Dictionary->count <<Std::endl; Std::cin.Get(); return 0;}
View Code
Http://docwiki.embarcadero.com/CodeExamples/XE6/en/Generics_Collections_TDictionary_%28C%2B%2B%29
Tdictionary is used by Delphi, C++builder is too laborious to use.