Visual C++ .NET/Collections/SortedDictionary

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Get key collection and value collection from SortedDictionary

<source lang="csharp">

  1. include "stdafx.h"
  2. using <system.dll>

using namespace System; using namespace System::Collections::Generic; ref class Reverse : public IComparer<int>{ public:

   virtual int Compare(int x, int y) { return y - x; }
   virtual bool Equals(int x, int y) { return x == y; }
   virtual int GetHashCode(int obj) { return obj.GetHashCode(); }

}; void SortedDictionaryExample(Dictionary<int,String^>^ inDict) {

   SortedDictionary<int,String^>^ dict = gcnew SortedDictionary<int,String^>(inDict, gcnew Reverse());
   
   SortedDictionary<int,String^>::KeyCollection::Enumerator ^key = dict->Keys->GetEnumerator();
   SortedDictionary<int,String^>::ValueCollection::Enumerator ^value = dict->Values->GetEnumerator();
   while ( key->MoveNext() && value->MoveNext())
   {
       Console::WriteLine("Key = [{0}]\tValue = [{1}]", 
           key->Current, value->Current);
   }

} void main() {

   Dictionary<int,String^>^ dict = gcnew Dictionary<int,String^>();
   dict->Add(1,  "One");
   dict->Add(6,  "Six");
   dict->Add(5,  "Five");
   dict->Add(3,  "3");
   dict[3] = "Three";
   dict[7] = "Seven";
       
   SortedDictionaryExample(dict);
   

}

 </source>