注释
社区兴趣团体现已从 Yammer 迁移到Microsoft Viva Engage。 若要加入 Viva Engage 社区并参与最新讨论,请填写 “请求访问财务和运营 Viva Engage 社区 ”表单,然后选择要加入的社区。
本文介绍容器运行时函数。
这些函数可作容器的内容。
conDel
从容器中删除指定数量的元素。
Syntax
container conDel(container container, int start, int number)
参数
| 参数 | Description |
|---|---|
| 容器 | 要从中删除元素的容器。 |
| 开始 | 要开始删除元素的基于一个位置。 |
| 数字 | 要删除的元素数。 |
返回值
不包含已删除元素的新容器。
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);
}
conFind
找到容器中元素的第一个匹配项。
Syntax
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
将一个或多个元素插入容器。
Syntax
container conIns(container container, int start, anytype element, ... )
参数
| 参数 | Description |
|---|---|
| 容器 | 要向其插入元素的容器。 |
| 开始 | 要插入元素的位置。 |
| 元素 | 要插入的一个或多个元素,用逗号分隔。 |
返回值
包含插入元素的新容器。
注解
容器的第一个元素由数字 1 指定。 若要在 n 元素之后插入, start 参数应为 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
检索容器中的元素数。
Syntax
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
从容器中检索特定元素,并根据需要将其转换为另一种数据类型。
Syntax
anytype conPeek(container container, int number)
参数
| 参数 | Description |
|---|---|
| 容器 | 要从中返回元素的容器。 |
| 数字 | 要返回的元素的位置。 指定 1 以获取第一个元素。 无效的位置号(例如 -3、 0 或大于容器长度的数字)可能会导致不可预知的错误。 |
返回值
容器中的元素位于 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
***/
conPoke
通过替换一个或多个现有元素来修改容器。
Syntax
container conPoke(container container, int start, anytype element, ...)
参数
| 参数 | Description |
|---|---|
| 容器 | 要修改的容器。 |
| 开始 | 要替换的第一个元素的位置。 |
| 元素 | 要替换的一个或多个元素,用逗号分隔。 |
返回值
包含新元素的新容器。
注解
容器的第一个元素由数字 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);
}