Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
1
Добавлен:
01.07.2025
Размер:
8.1 Mб
Скачать

37.2.5Удаление элементов хеш-таблицы

To delete one item from the list, you can call the Remove() method. Its syntax is:

public virtual void Remove(object key);

Here is an example:

using namespace System;

using namespace System::Collections;

int main()

{

Hashtable ^ Chelsea20062007 = gcnew Hashtable;

Chelsea20062007->Add(L"Michael", L"Ballack");

Chelsea20062007->Add(L"Didier", L"Drogba");

Chelsea20062007[L"Claude"] = L"Makelele";

Console::WriteLine(L"List of items");

for each(DictionaryEntry ^ entry in Chelsea20062007)

Console::WriteLine(L"{0} {1}", entry->Key, entry->Value);

Chelsea20062007->Remove(L"Didier");

Console::WriteLine(L"\nThe item that contains "

L"Didier has been removed\n");

Console::WriteLine(L"List of items");

for each(DictionaryEntry ^ entry in Chelsea20062007)

Console::WriteLine(L"{0} {1}", entry->Key, entry->Value);

Console::WriteLine();

return 0;

}

}

}

This would produce:

List of items

Didier Drogba

Michael Ballack

Claude Makelele

The item that contains Didier has been removed

List of items

Michael Ballack

Claude Makelele

Press any key to continue . . .

To delete all items from the list, you can call the Clear() method. Its syntax is:

public virtual void Clear();

This is an example:

using namespace System;

using namespace System::Collections;

int main()

{

Hashtable ^ Chelsea20062007 = gcnew Hashtable;

Chelsea20062007->Add(L"Michael", L"Ballack");

Chelsea20062007->Add(L"Didier", L"Drogba");

Chelsea20062007[L"Claude"] = L"Makelele";

Console::WriteLine(L"List of items");

for each(DictionaryEntry ^ entry in Chelsea20062007)

Console::WriteLine(L"{0} {1}", entry->Key, entry->Value);

Chelsea20062007->Clear();

Console::WriteLine(L"\nThe list is now empty!");

Console::WriteLine(L"List of items");

for each(DictionaryEntry ^ entry in Chelsea20062007)

Console::WriteLine(L"{0} {1}", entry->Key, entry->Value);

Console::WriteLine();

return 0;

}

}

}

This would produce:

List of items

Didier Drogba

Michael Ballack

Claude Makelele

The list is now empty!

List of items

Press any key to continue . . .

37.3Стек

37.3.1 Вступление

A stack is a technique of creating a list so that the last item added to the list is also the first one that can be removed. It can be illustrated as placing a few cups in a box that can receive only one on top of another (no adjacent cup). When it comes time to get one of those cups, you must first access the last one that was added. This technique of building a list is referred to as first-in last-out (FILO).

To support stack types of collections, the .NET Framework provides the Stack class. Stack is a serializable class that implements the ICollection (giving the ability to know the number of items in the list) and the IEnumerable (which gives the ability to use foreach).