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

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

Текущая версия на 12:05, 26 мая 2010

Add key and value to Hashtable

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void main(){
    Hashtable ^hash  = gcnew Hashtable();
    SortedList ^sort = gcnew SortedList();
    array<String^>^ keys   = gcnew array<String^> { "B", "A", "C", "D" };
    array<String^>^ skeys  = gcnew array<String^> { "A", "B", "C", "D" };
    array<String^>^ values = gcnew array<String^> { "a", "b", "c", "d" };
    for (int i = 0; i < keys->Length; i++)
    {
        hash->Add(keys[i], values[i]);
        sort->Add(keys[i], values[i]);
    }
}


Get Enumerator from Hashtable

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void main(){
    Hashtable ^hash  = gcnew Hashtable();
    SortedList ^sort = gcnew SortedList();
    array<String^>^ keys   = gcnew array<String^> { "B", "A", "C", "D" };
    array<String^>^ skeys  = gcnew array<String^> { "A", "B", "C", "D" };
    array<String^>^ values = gcnew array<String^> { "a", "b", "c", "d" };
    for (int i = 0; i < keys->Length; i++)
    {
        hash->Add(keys[i], values[i]);
        sort->Add(keys[i], values[i]);
    }
    Console::WriteLine("\nBy enumerator");
    IDictionaryEnumerator ^enum1 = hash->GetEnumerator();
    IDictionaryEnumerator ^enum2 = sort->GetEnumerator();
    while ( enum1->MoveNext() && enum2->MoveNext())
    {
        Console::Write("{0} {1}\t\t", enum1->Key, enum1->Value);
        Console::WriteLine("{0} {1}", enum2->Key, enum2->Value);
    }
}


Get Key enumerator from Hashtable

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void main(){
    Hashtable ^hash  = gcnew Hashtable();
    SortedList ^sort = gcnew SortedList();
    array<String^>^ keys   = gcnew array<String^> { "B", "A", "C", "D" };
    array<String^>^ skeys  = gcnew array<String^> { "A", "B", "C", "D" };
    array<String^>^ values = gcnew array<String^> { "a", "b", "c", "d" };
    for (int i = 0; i < keys->Length; i++)
    {
        hash->Add(keys[i], values[i]);
        sort->Add(keys[i], values[i]);
    }
    Console::WriteLine("\nEnumerate Key");
    IEnumerator ^keys1 = hash->Keys->GetEnumerator();
    IEnumerator ^keys2 = sort->Keys->GetEnumerator();
    while ( keys1->MoveNext() && keys2->MoveNext())
    {
        Console::Write("{0}\t\t", keys1->Current);
        Console::WriteLine("{0}", keys2->Current);
    }
}


Get value enumerator from Hashtable

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void main(){
    Hashtable ^hash  = gcnew Hashtable();
    SortedList ^sort = gcnew SortedList();
    array<String^>^ keys   = gcnew array<String^> { "B", "A", "C", "D" };
    array<String^>^ skeys  = gcnew array<String^> { "A", "B", "C", "D" };
    array<String^>^ values = gcnew array<String^> { "a", "b", "c", "d" };
    for (int i = 0; i < keys->Length; i++)
    {
        hash->Add(keys[i], values[i]);
        sort->Add(keys[i], values[i]);
    }
    Console::WriteLine("\nEnumerate Value");
    IEnumerator ^vals1 = hash->Values->GetEnumerator();
    IEnumerator ^vals2 = sort->Values->GetEnumerator();
    while ( vals1->MoveNext() && vals2->MoveNext())
    {
        Console::Write("{0}\t\t", vals1->Current);
        Console::WriteLine("{0}", vals2->Current);
    }
}


Get value from Hashtable by indexed property

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void main(){
    Hashtable ^hash  = gcnew Hashtable();
    SortedList ^sort = gcnew SortedList();
    array<String^>^ keys   = gcnew array<String^> { "B", "A", "C", "D" };
    array<String^>^ skeys  = gcnew array<String^> { "A", "B", "C", "D" };
    array<String^>^ values = gcnew array<String^> { "a", "b", "c", "d" };
    for (int i = 0; i < keys->Length; i++)
    {
        hash->Add(keys[i], values[i]);
        sort->Add(keys[i], values[i]);
    }
    Console::WriteLine("By indexed property");
    for (int i = 0; i < hash->Count; i++)
    {
        Console::WriteLine("{0} {1}\t\t{2} {3}", skeys[i], 
            hash[skeys[i]], skeys[i], sort[skeys[i]]);
    }
}


Hashtable contains key

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void main(){
    Hashtable ^hash  = gcnew Hashtable();
    SortedList ^sort = gcnew SortedList();
    array<String^>^ keys   = gcnew array<String^> { "B", "A", "C", "D" };
    array<String^>^ skeys  = gcnew array<String^> { "A", "B", "C", "D" };
    array<String^>^ values = gcnew array<String^> { "a", "b", "c", "d" };
    for (int i = 0; i < keys->Length; i++)
    {
        hash->Add(keys[i], values[i]);
        sort->Add(keys[i], values[i]);
    }
    Console::WriteLine("\nContains a Key "A" and "Z"");
    Console::WriteLine("{0}\t\t{1}", hash->Contains("A"), sort->Contains("A")); 
    Console::WriteLine("{0}\t\t{1}", hash->ContainsKey("Z"), sort->ContainsKey("Z")); 
}


Hashtable contains value

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void main(){
    Hashtable ^hash  = gcnew Hashtable();
    SortedList ^sort = gcnew SortedList();
    array<String^>^ keys   = gcnew array<String^> { "B", "A", "C", "D" };
    array<String^>^ skeys  = gcnew array<String^> { "A", "B", "C", "D" };
    array<String^>^ values = gcnew array<String^> { "a", "b", "c", "d" };
    for (int i = 0; i < keys->Length; i++)
    {
        hash->Add(keys[i], values[i]);
        sort->Add(keys[i], values[i]);
    }
    Console::WriteLine("\nContains a Value "a" and "c"");
    Console::WriteLine("{0}\t\t{1}", hash->ContainsValue("a"), sort->ContainsValue("a")); 
    Console::WriteLine("{0}\t\t{1}", hash->ContainsValue("c"), sort->ContainsValue("c")); 
}