Visual C++ .NET/Collections/SortedList
Версия от 15:31, 26 мая 2010; (обсуждение)
Содержание
- 1 Add key and value to SortedList
- 2 Get Enumerator from SortedList
- 3 Get key enumerator from SortedList
- 4 Get the value index from a SortedList
- 5 Get value enumerator from SortedList
- 6 Get value from SortedList by indexed property
- 7 Get value in SortedList by index
- 8 SortedList contains key
- 9 SortedList contains value
- 10 SortedList indexing of Key
Add key and value to SortedList
#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 SortedList
#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 SortedList
#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 the value index from a SortedList
#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("\n\t\t"B" key index: {0}", sort->IndexOfKey("B"));
Console::WriteLine("\t\t"frog" value index: {0}", sort->IndexOfValue("frog"));
}
Get value enumerator from SortedList
#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 SortedList 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]]);
}
}
Get value in SortedList by index
#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 index");
for (int i = 0; i < sort->Count; i++)
{
Console::WriteLine("N/A\t\t{0} {1}", i, sort->GetByIndex(i));
}
}
SortedList 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"));
}
SortedList 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"));
}
SortedList indexing of 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("\n\t\t"B" key index: {0}", sort->IndexOfKey("B"));
Console::WriteLine("\t\t"frog" value index: {0}", sort->IndexOfValue("frog"));
}