Visual C++ .NET/Collections/Dictionary
Содержание
- 1 Adding value pair to a dictionary
- 2 Adding value to a dictionary with indexer
- 3 Create SortedDictionary from a Dictionary
- 4 Does a Dictionary contain a certain key
- 5 Get Key Collection Enumerator and Value Collection Enumerator
- 6 Get KeyValuePair from Dictionary
- 7 Get value by Dictionary indexer
- 8 Get values from a Dictionary
- 9 Pass Dictionary to a function
- 10 The add method of dictionary takes the key and the value
- 11 The remove method of dictionary takes the key as an argument
- 12 Using for each to loop through a dictionary
Adding value pair to a dictionary
<source lang="csharp">
- 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");
}
</source>
Adding value to a dictionary with indexer
<source lang="csharp">
- 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";
}
</source>
Create SortedDictionary from a Dictionary
<source lang="csharp">
- 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);
}
</source>
Does a Dictionary contain a certain key
<source lang="csharp">
- 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));
}
</source>
Get Key Collection Enumerator and Value Collection Enumerator
<source lang="csharp">
- 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); }
}
</source>
Get KeyValuePair from Dictionary
<source lang="csharp">
- 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); }
}
</source>
Get value by Dictionary indexer
<source lang="csharp">
- 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);
}
</source>
Get values from a Dictionary
<source lang="csharp">
- 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);
}
</source>
Pass Dictionary to a function
<source lang="csharp">
- 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);
}
</source>
The add method of dictionary takes the key and the value
<source lang="csharp">
- 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">
- 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">
- 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); }
}
</source>