dlang语言最新dmd2103版本关于数组使用的介绍文章链接为httpsdlangorgphobosstd_arrayhtml我提取了下关键文字信息如下stdarraystableFunctions and types that manipulate built-in arrays and associative arraysThis module provides all kinds of f
关键知识点:
- std.array模块提供了许多操作内置数组和关联数组的函数和类型;
- 可以使用array函数创建一个新的动态数组;
- 使用appender函数可以创建一个新的Appender或RefAppender对象,并将其初始化为给定数组;
- 使用assocArray函数可以从一系列键/值元组的范围中创建一个新的关联数组;
- 使用insertInPlace函数可以在给定的位置向现有数组中插入元素;
- 使用join函数可以将一系列范围连接成一个数组;
- 使用replace函数可以创建一个新的数组,其中所有特定子范围的元素都被替换;
- 使用replicate函数可以使用输入数组或范围的多个副本创建一个新数组;
- 使用split函数可以将一个范围或字符串急切地分成一个数组;
- 使用staticArray函数可以从给定数据创建一个新的静态数组;
- uninitializedArray函数可以创建一个新的类型为T的数组,而不初始化其元素。
完整例子:
- 创建一个新的动态数组并打印出来:
import std.array;
import std.stdio;
void main()
{
int[] arr = [1, 2, 3];
int[] newArr = array(arr);
writeln(newArr); // 输出[1, 2, 3]
}
- 创建一个新的关联数组并打印出来:
import std.array;
import std.stdio;
void main()
{
auto tuples = ["apple": 1, "banana": 2, "orange": 3];
auto assocArr = assocArray(tuples);
writeln(assocArr); // 输出["apple": 1, "banana": 2, "orange": 3]
}
- 将元素插入到现有数组中并打印出来:
import std.array;
import std.stdio;
void main()
{
int[] arr = [1, 2, 3];
insertInPlace(arr, 1, 4);
writeln(arr); // 输出[1, 4, 2, 3]
}
- 将两个数组连接起来并打印出来:
import std.array;
import std.stdio;
void main()
{
int[] arr1 = [1, 2, 3];
int[] arr2 = [4, 5, 6];
auto joinedArr = join([arr1, arr2]);
writeln(joinedArr); // 输出[1, 2, 3, 4, 5, 6]
}
``
原文地址: http://www.cveoy.top/t/topic/fj7T 著作权归作者所有。请勿转载和采集!