Visual C++ .NET/Collections/Dictionary — различия между версиями

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

Версия 15:31, 26 мая 2010

Adding value pair to a dictionary

 
#include "stdafx.h"
#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");
}


Adding value to a dictionary with indexer

 
#include "stdafx.h"
#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";
}


Create SortedDictionary from a Dictionary

 
#include "stdafx.h"
#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);
    
}


Does a Dictionary contain a certain key

 
#include "stdafx.h"
#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));
    
}


Get Key Collection Enumerator and Value Collection Enumerator

 
#include "stdafx.h"
#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);
    }
    
}


Get KeyValuePair from Dictionary

 
#include "stdafx.h"
#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);
    }
    
}


Get value by Dictionary indexer

 
#include "stdafx.h"
#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);
    
}


Get values from a Dictionary

 
#include "stdafx.h"
#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);
    
}


Pass Dictionary to a function

 
#include "stdafx.h"
#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);
    
}


The add method of dictionary takes the key and the value

 
#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);
   }
}


The remove method of dictionary takes the key as an argument

 
#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);
   }
}


Using for each to loop through a dictionary

 
#include "stdafx.h"
#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);
    }
    
}