Visual C++: Afficher le contenu d’un tableau associatif HashTable

Author:


Download

 
#include "stdafx.h"
 
using namespace System;
using namespace System::Collections;
/*
  Exemple de Tableau Associatif.
  MesExemples.com
*/
 
Int32 main(void)
{
	try
	{
		// Créer un objet Hashtable
		Hashtable ^hash  = gcnew Hashtable();
 
		 // Créer tableaux de clé et valeurs
		 array<String ^> ^keys = gcnew array<System::String ^>{ "fr", "en", "gr", "de" }; // Représentent les codes de pays
         array<String ^> ^values = gcnew array<String ^>{ "français", "anglais", "grec", "allemand" };// Représentent les pays
 
		 // Combiner les deux tableaux dans un tableau associatif HashTable
		     for (Int32 i = 0; i < keys->Length; i++)
		   {
				hash->Add(keys[i], values[i]);
			}
 
 
		    // Contenu de Hashtable par index
		Console::WriteLine("Contenu par Index");
		// Parcourir le contenu à l'aide dun boucle 'for'
		for (Int32 i = 0; i < hash->Count; i++)
		{
        Console::WriteLine("{0}==>{1}", keys[i],
            hash->default[keys[i]]);
		}
 
 
 
	}  catch (Exception ^e)
    {
        Console::WriteLine(e->StackTrace);
    }
    Console::ReadLine();
    return 0;
}
 
 
/*
Contenu par Index
fr==>français
en==>anglais
gr==>grec
de==>allemand
*/