目录
isvalidindex —— 检测指定的索引在数组或字符串中是否有效
array —— 高效创建数组
[] array(...)
- //结果为{{1,1,1}, {1,2,3}, {3,3,3}}
- vector v[] = vector[](array(1, {1,2,3}, 3));
- //结果为{1, 1, 3}
- float v[] = float[](array(1, {1,2,3}, 3));
len —— 返回数组等长度
int len(v) int len(m) int len(array[]) int len(string s) int len(dict d)
- length函数是计算vector大小;
isvalidindex —— 检测指定的索引在数组或字符串中是否有效
//在数组或字符串范围内返回1,否则返回0; //index < len(array) && index >= -len(array) int isvalidindex(&array[], int index) int isvalidindex(string str, int index)
//如key在字典内返回1,否则返回0 int isvalidindex(dict d, string key)
append —— 追加元素到数组或字符串
- //字符串追加
- void append(string &array, string value)
- //数组追加,等价于 push(array, value)
- void append(
&array[], value) - //追加(或连接)数组,等价于 push(array, values)
- void append(
&array[], values[])
- string s = "abcd";
- append(s, 'efg');
- int arr[] = {0,1,2};
- append(arr, 3);
push —— 添加元素到数组
- //等价于 append(array, value)
- void push(
&array[], value) - //等价于 append(array, values)
- void push(
&array[], values[])
upush —— 添加统一值到数组
- //对所有SIMD程序
- void upush(
&array[], value)
pop —— 移除数组最后一个元素并返回
- //移除最后一个元素
pop(&array[]) - //移除指定元素
pop(&array[], int index)
insert —— 插入元素、数组、字符串
//插入字符串,如索引大于长度则追加末尾 void insert(string &str, int index, string value)
//插入数组 //如索引超过长度则使用0或空 //如索引为负值,从数组末尾开始,大于长度则截止到0 void insert(&array[], int index, value) void insert(&array[], int index, values[])
//复制srcdict[srckey]值到dstdict[dstkey] //如srcdict内不存在srckey,则将从dstdict内移除 //key存在返回1,否则返回0 int insert(dict &dstdict, string dstkey, dict srcdict, string srckey)
//将srcdict合并到dstdict,相同的key将覆盖 void insert(dict &dstdict, dict srcdict)
removeindex —— 移除指定位置的元素
//移除指定位置元素并返回其值 //等价于pop(array, index)removeindex( &array[], int index)
//移除字典entry,无entry返回0,否则返回1 int removeindex(dict &dictionary, string index)
removevalue —— 移除指定元素
- //移除首个匹配的元素,移除返回1,否则返回0
- int removevalue(
&array[], value)
- float nums[] = {0, 1, 2, 3, 1, 2, 3};
- removevalue(nums; 2); // == 1
- // nums == {0, 1, 3, 1, 2, 3}
sort —— 返回升序后的数组
- int [] sort(int values[])
- float [] sort(float values[])
- string [] sort(string values[])
argsort —— 返回数组排序后的索引
int[] argsort(value[])
- //通常元素长度,排序字符串
- cvex main()
- {
- // Given an array of strings...
- string colors[] = {"Red", "Green", "Blue", "Orange", "Violet", "Indigo"};
-
- // Create an array with the corresponding lengths
- int[] lengths = {};
- foreach (string name; colors) {
- push(lengths, len(name));
- }
-
- // Sort the lengths and return an array containing the new ordering
- int[] ordering = argsort(lengths);
-
- // Get the array of color names but sorted by name length
- string colors_by_len[] = reorder(colors, ordering);
-
- printf("%s\n", colors_by_len);
- }
reorder —— 重新排序数组或字符串
- //UTF-8字符串
- string reorder(string value, int indices[])
- //数组
[] reorder(values[], int indices[])
resize —— 重置数组长度
//调整尺寸,如大于当前长度则使用0或空 void resize(&array[], int size)
//调整尺寸,如大于当前长度则使用指定的val void resize(&array[], int size, val)
reverse —— 返回反转数组和字符串
//返回反转UTF-8字符串,与str[::-1](表示从开头到结尾步幅为-1); string reverse(string str)
//返回反转的数组[] reverse( values[])
slice —— 对数组或字符串进行切片
//提取子字符串 string slice(string s, int start, int end) string slice(string s, int start, int end, int step)
//提取子数组[] slice( s[], int start, int end) [] slice( s[], int start, int end, int step)
//通用规则 string slice(string s, int hasstart, int start, int hasend, int end, int hasstep, int step)[] slice( array[], int hasstart, int start, int hasend, int end, int hasstep, int step)
- int nums[] = {10, 20, 30, 40, 50, 60};
- slice(nums, 1, 3) == {20, 30} // nums[1:3]
- slice(nums, 1, -1) == {20, 30, 40, 50} // nums[1:-1]
- slice(nums, 0, 6, 2) == {10, 30, 50} // nums[0:6:2]
- slice(nums, 0, 0, 0, 0, 1, 2) == {10, 30, 50} // nums[::2]
foreach —— 循环数组内的元素
- //针对array中的每个成员value,计算语句;
- //可选index,为每次计算的当前位置;
- foreach(value; array) statement
- foreach(index; value; array) statement