共用方式為


X++ 容器執行階段函式

備註

社區興趣小組現在已從 Yammer 轉移到 Microsoft Viva Engage。 若要加入 Viva Engage 社群並參與最新的討論,請填寫 [ 要求存取財務和營運 Viva Engage 社群 表單 」 ,並選擇您要加入的社群。

本文說明容器執行時間函式。

這些函式會操作容器的內容。

conDel

從容器中移除指定數量的元素。

語法

container conDel(container container, int start, int number)

參數

參數 Description
容器 要從中移除元素的容器。
start 開始移除元素的以一為基礎的位置。
數字 要刪除的元素數目。

返回值

不包含已移除元素的新容器。

Example

static void conDelExample(Args _args)
{
    container c = ["Hello world", 1, 3.14];
    
    // Deletes the first two items from the container.
    c = conDel(c, 1, 2);
}

發現

在容器中尋找第一個元素出現的位置。

語法

int conFind(container container, anytype element)

參數

參數 Description
容器 要搜尋的容器。
元素 要搜尋的專案。

返回值

如果找不到項目,則為 0;否則,項目的序號。

Example

static void conFindExample(Args _args)
{
    container c = ["item1", "item2", "item3"];
    int i = conFind(c, "item2");
    int j = conFind(c, "item4");

    print "Position of 'item2' in container is " + int2Str(i);
    print "Position of 'item4' in container is " + int2Str(j);
}

conIns

將一或多個元素插入容器中。

語法

container conIns(container container, int start, anytype element, ... )

參數

參數 Description
容器 要插入元素的容器。
start 要插入元素的位置。
元素 要插入的一或多個元素,以逗號分隔。

返回值

包含插入元素的新容器。

備註

容器的第一個元素由數字 1 指定。 若要插入 n 元素之後, 開始 參數應為 n+1。 您也可以使用 += 運算子將任何類型的值新增至容器。 例如,若要建立包含前 10 次迴圈反覆專案平方值的容器,請使用下列程式碼。

int i;
container c;

for (i = 1; i < = 10; i++)
{
    // Append the square of the index to the container
    c += i*i;
}

Example

static void conInsExample(Args _arg)
{
    container c;
    int i;

    c = conIns(c,1,"item1");
    c = conIns(c,2,"item2");

    for (i = 1 ; i <= conLen(c) ; i++)
    {
        // Prints the content of a container.
        print conPeek(c, i);
    }
}

conLen

擷取容器中的元素數目。

語法

int conLen(container container)

參數

參數 Description
容器 要計算元素數的容器。

返回值

容器中的元素數目。 conNull 容器沒有元素。

Example

static void conLenExample(Args _arg)
{
    container c = conins(["item1", "item2"], 1);

    for (int i = 1 ; i <= conLen(c) ; i++)
    {
        print conPeek(c, i);
    }
}

conNull

擷取空白容器。

container conNull()

返回值

一個空容器。

Example

static void conNullExample(Args _arg)
{
    container c = ["item1", "item2", "item3"];

    print "The size of container is " + int2str(conLen(c));

    // Set the container to null.
    c = conNull();
    print "Size of container after conNull() is " + int2Str(conLen(c));
}

conPeek

從容器擷取特定元素,並將其轉換為另一種資料類型(如果需要轉換)。

語法

anytype conPeek(container container, int number)

參數

參數 Description
容器 要從中傳回元素的容器。
數字 要傳回的元素位置。 指定 1 以取得第一個元素。 無效的位置號碼 (例如 -30 或高於容器長度的數字) 可能會導致無法預測的錯誤。

返回值

容器中 number 參數所指定位置的元素。 conPeek 函式會自動將已偷看的專案轉換成預期的傳回類型。 字串可以自動轉換為整數和實數,整數和實數可以轉換為字串。

Example

static void main(Args _args)
{
    container cnI, cnJ;
    int i, j;
    anytype aty;
    
    info("container cnI ...");
    cnI = ["itemBlue", "itemYellow"];

    for (i=1; i <= conLen(cnI); i++)
    {
        aty = conPeek(cnI, i);
        info(int2str(i) + " :  " + aty);
    }

    info("container cnJ ...");
    cnJ = conIns(cnI, 2, "ItemInserted");

    for (j=1; j <= conLen(cnJ); j++)
    {
        aty = conPeek(cnJ, j);
        info(int2str(j) + " :  " + aty);
    }
}
/***  Output pasted from InfoLog ...
Message (10:20:03 am)
container cnI ...
1 :  itemBlue
2 :  itemYellow
container cnJ ...
1 :  itemBlue
2 :  ItemInserted
3 :  itemYellow
***/

共戳

透過取代一或多個現有元素來修改容器。

語法

container conPoke(container container, int start, anytype element, ...)

參數

參數 Description
容器 要修改的容器。
start 要取代的第一個元素的位置。
元素 要取代的一或多個元素,以逗號分隔。

返回值

包含新元素的新容器。

備註

容器的第一個元素由數字 1 指定。

Example

static void conPokeExample(Args _arg)
{
    container c1 = ["item1", "item2", "item3"];
    container c2;
    int i;

    void conPrint(container c)
    {
        for (i = 1 ; i <= conLen(c) ; i++)
        {
            print conPeek(c, i);
        }
    }

    conPrint(c1);
    c2 = conPoke(c1, 2, "PokedItem");
    print "";
    conPrint(c2);
}