Visual C++ .NET/Collections/Dictionary

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

Adding value pair to a dictionary

<source lang="csharp">

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

using namespace System; using namespace System::Collections::Generic;

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");

}

 </source>


Adding value to a dictionary with indexer

<source lang="csharp">

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

using namespace System; using namespace System::Collections::Generic;

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";

}

 </source>


Create SortedDictionary from a Dictionary

<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());
   
   dict->Add(6,  "Six");
   String^ t = dict[3];
   Console::WriteLine("dict[3] = {0}\n", t);

} 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>


Does a Dictionary contain a certain key

<source lang="csharp">

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

using namespace System; using namespace System::Collections::Generic;

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";
       
   Console::WriteLine("\nDictionary contains 6? [{0}]", dict->ContainsKey(6));
   

}

 </source>


Get Key Collection Enumerator and Value Collection Enumerator

<source lang="csharp">

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

using namespace System; using namespace System::Collections::Generic;

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";
       
   Dictionary<int,String^>::KeyCollection::Enumerator ^key = dict->Keys->GetEnumerator();
   Dictionary<int,String^>::ValueCollection::Enumerator ^value = dict->Values->GetEnumerator();
   while ( key->MoveNext() && value->MoveNext()){
       Console::WriteLine("Key = [{0}]\tValue = [{1}]", key->Current, value->Current);
   }
   

}

 </source>


Get KeyValuePair from Dictionary

<source lang="csharp">

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

using namespace System; using namespace System::Collections::Generic;

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";
       
   for each (KeyValuePair<int,String^>^ pair in dict)
   {
       Console::WriteLine("Key = [{0}]\tValue = [{1}]", 
           pair->Key, pair->Value);
   }
   

}

 </source>


Get value by Dictionary indexer

<source lang="csharp">

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

using namespace System; using namespace System::Collections::Generic;

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";
       
   String^ t = dict[3];
   Console::WriteLine("dict[3] = {0}\n", t);
   

}

 </source>


Get values from a Dictionary

<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());
   
   dict->Add(6,  "Six");
   String^ t = dict[3];
   Console::WriteLine("dict[3] = {0}\n", t);
   Console::WriteLine("Sorted Values:");
   for each (String ^s in dict->Values)
       Console::WriteLine("\t{0}",s);

} 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>


Pass Dictionary to a function

<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());
   
   dict->Add(6,  "Six");
   String^ t = dict[3];
   Console::WriteLine("dict[3] = {0}\n", t);

} 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>


The add method of dictionary takes the key and the value

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections::Generic; int main() {

  IDictionary<String^, String^>^ dict;
  dict = gcnew Dictionary<String^, String^>();
  dict->Add("a", "A");
  dict->Add("o", "B");
  dict->Add("i", "C");
  for each (KeyValuePair<String^, String^>^ pair in dict)
  {
     Console::WriteLine(" {0}: {1}", pair->Key, pair->Value);
  }
  dict->Remove("hat");
  for each (KeyValuePair<String^, String^>^ pair in dict)
  {
     Console::WriteLine(" {0}: {1}", pair->Key, pair->Value);
  }

}

 </source>


The remove method of dictionary takes the key as an argument

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections::Generic; int main() {

  IDictionary<String^, String^>^ dict;
  dict = gcnew Dictionary<String^, String^>();
  dict->Add("a", "A");
  dict->Add("o", "B");
  dict->Add("i", "C");
  for each (KeyValuePair<String^, String^>^ pair in dict)
  {
     Console::WriteLine(" {0}: {1}", pair->Key, pair->Value);
  }
  dict->Remove("hat");
  for each (KeyValuePair<String^, String^>^ pair in dict)
  {
     Console::WriteLine(" {0}: {1}", pair->Key, pair->Value);
  }

}

 </source>


Using for each to loop through a dictionary

<source lang="csharp">

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

using namespace System; using namespace System::Collections::Generic;

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";
       
   for each (KeyValuePair<int,String^>^ pair in dict)
   {
       Console::WriteLine("Key = [{0}]\tValue = [{1}]", pair->Key, pair->Value);
   }
   

}

 </source>