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

34.3.5The Beginning of a Collection

A good collection is a list that can grow or shrink as the user wishes. When creating the list, you don't need to predict the maximum number of items that will be added to the list. When a list starts, it is empty or at least it must be considered like that, before any item is added to it. To specify this, you should declare a primary member variable. Although you can call it anything, it is usually called Head. This member can be made private if you don't intend to access it outside of the class. If you want clients of the class to access it, you can make it public. Here is an example:

public ref class CNumbers

{

private:

int size;

CNumber ^ Sample;

public:

CNumber ^ Head;

CNumbers()

{

size = 0;

Head = nullptr;

}

property int Count

{

int get() { return size; }

}

};

34.3.6 Linking the Items of a Collection

When using an array, each member can be accessed using its index. If the items of a collection are not indexed, you must provide a mechanism to locate an item. To do this, you can use a starting point that determines the beginning of the first. Then, to locate an item, you can check whether another item follows that starting point. If no item follows it, either you are at the end of the list or the list is empty.

To be able to scan a list from one item to another, you can declare a member variable. Although this member variable can be called anything, for the sake of clarify, you should call it Next. The member variable is the same type as its class. Here is an example:

public ref class CNumber

{

public:

double Item;

CNumber ^ Next;

};

34.3.7 Practical Learning: Creating a List's Monitor

  1. Access the Flower.h header file and change it as follows:  

    using namespace System;

    . . . No Change

    public ref class CFlower

    {

    . . . No Change

    protected:

    int items;

    public:

    property int Count

    {

    int get() { return items; }

    }

    CFlower ^ Next;

    };

  2. Access the FlowerInventory.h header file and change it as follows:  

    #pragma once

    #include "Flower.h"

    public ref class CFlowerInventory : CFlower

    {

    public:

    CFlower ^ Inventory;

    CFlowerInventory(void);

    CFlower ^ Head;

    };

  3. Access the FlowerInventory.cpp source file and change it as follows:  

    #include "FlowerInventory.h"

    CFlowerInventory::CFlowerInventory(void)

    {

    Head = nullptr;

    Inventory = gcnew CFlower;

    }

  4. Save all

34.4 Коллекции на основе списка (Operations on a Collection)

34.4.1 Adding an Item

The primary operation you can perform on a list is to add a new item to it, since a list is fundamentally empty when it starts. In order to indicate that you want to add an item to the list, you can create a method that receives an item as argument. For the return type, you have two main options. Because the main job of this method is to add a new item, which it hardly fails to do if you implement it right, it can be defined as void. Alternatively, you can make it return the position of the new item in the list. Here is an example:

public ref class CNumbers

{

private:

int size;

CNumber ^ Sample;

public:

CNumber ^ Head;

CNumbers()

{

size = 0;

Head = nullptr;

}

property int Count

{

int get() { return size; }

}

int Add(CNumber ^ NewItem)

{

CNumber ^ Sample = gcnew CNumber;

Sample = NewItem;

Sample->Next = Head;

Head = Sample;

return size++;

}

};

int main()

{

CNumbers ^ nbrs = gcnew CNumbers;

Console::WriteLine(L"Number of Items: {0}", nbrs->Count);

CNumber ^ one = gcnew CNumber;

one->Item = 2937.45;

nbrs->Add(one);

one = gcnew CNumber;

one->Item = 329.459;

nbrs->Add(one);

one = gcnew CNumber;

one->Item = 98.23456;

nbrs->Add(one);

one = gcnew CNumber;

one->Item = 32465.645;

nbrs->Add(one);

return 0;

}