This commit is contained in:
krahets
2023-06-25 21:11:35 +08:00
parent 5bc8df6d5d
commit e4e6cd6bae
19 changed files with 836 additions and 105 deletions

View File

@@ -433,7 +433,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
fn extend(mem_allocator: std.mem.Allocator, nums: []i32, enlarge: usize) ![]i32 {
// 初始化一个扩展长度后的数组
var res = try mem_allocator.alloc(i32, nums.len + enlarge);
std.mem.set(i32, res, 0);
@memset(res, 0);
// 将原数组中的所有元素复制到新数组
std.mem.copy(i32, res, nums);
// 返回扩展后的新数组
@@ -1077,7 +1077,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
```zig title="array.zig"
// 在数组中查找指定元素
fn find(nums: []i32, target: i32) i32 {
for (nums) |num, i| {
for (nums, 0..) |num, i| {
if (num == target) return @intCast(i32, i);
}
return -1;