Tdictionary is used by Delphi, C++builder is too laborious to use.

Source: Internet
Author: User

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:

    1. Right-click your Cppapplication project on the project Manager and select Add.
    2. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.