Array concatenation: Difference between revisions
Content added Content deleted
imported>Belowdecent |
Recombinant (talk | contribs) (→{{header|Zig}}: fix, add deinit() where appropriate, use std.mem.concat) |
||
Line 4,700: | Line 4,700: | ||
=={{header|Zig}}== |
=={{header|Zig}}== |
||
There are no hidden memory allocations in Zig. |
|||
<syntaxhighlight lang="zig"> |
<syntaxhighlight lang="zig"> |
||
fn concat(allocator: std.mem.Allocator, a: []const u32, b: []const u32) ![]u32 { |
|||
const std = @import("std"); |
|||
std.mem.copy(u32, result, a); |
|||
std.mem.copy(u32, result[a.len..], b); |
|||
return result; |
|||
⚫ | |||
pub fn main() void { |
pub fn main() !void { |
||
var |
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
||
defer _ = gpa.deinit(); |
|||
const gpa = general_purpose_allocator.allocator(); |
|||
⚫ | |||
const allocator = gpa.allocator(); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
defer allocator.free(slice3); |
|||
// Same result, alternative syntax |
|||
const slice4 = try std.mem.concat(allocator, u32, &[_][]const u32{ array1[0..], array2[0..] }); |
|||
defer allocator.free(slice4); |
|||
std.debug.print( |
|||
⚫ | |||
.{ array1, array2, slice3, slice4 }, |
|||
); |
|||
⚫ | |||
⚫ | |||
{{out}} |
|||
<pre> |
|||
Array 1: { 1, 2, 3, 4, 5 } |
|||
Array 2: { 6, 7, 8, 9, 10, 11, 12 } |
|||
Slice 3: { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 } |
|||
Slice 4: { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 } |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |