File tree Expand file tree Collapse file tree 4 files changed +79
-0
lines changed Expand file tree Collapse file tree 4 files changed +79
-0
lines changed Original file line number Diff line number Diff line change
1
+ const std = @import ("std" );
2
+ const print = std .debug .print ;
3
+
4
+ pub fn main () ! void {
5
+ var gpa = std .heap .GeneralPurposeAllocator (.{}){};
6
+ defer _ = gpa .deinit ();
7
+
8
+ const allocator = gpa .allocator ();
9
+ var map = std .AutoHashMap (u8 , []const u8 ).init (allocator );
10
+ defer map .deinit ();
11
+
12
+ // Insert
13
+ try map .put (0 , "Zero" );
14
+ try map .put (1 , "One" );
15
+ try map .put (2 , "Two" );
16
+
17
+ const value = map .get (1 );
18
+ if (value ) | v | {
19
+ print ("{s}" , .{v });
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ const std = @import ("std" );
2
+ const print = std .debug .print ;
3
+
4
+ pub fn main () ! void {
5
+ var gpa = std .heap .GeneralPurposeAllocator (.{}){};
6
+ defer _ = gpa .deinit ();
7
+
8
+ const allocator = gpa .allocator ();
9
+ var map = std .AutoHashMap (u8 , []const u8 ).init (allocator );
10
+ defer map .deinit ();
11
+
12
+ // Insert
13
+ try map .put (0 , "Zero" );
14
+ try map .put (1 , "One" );
15
+ try map .put (2 , "Two" );
16
+
17
+ const value = map .get (99 ) orelse "Default" ;
18
+ print ("{s}" , .{value });
19
+ }
Original file line number Diff line number Diff line change
1
+ # Hash Maps
2
+
3
+ key-value pair
4
+
5
+ ``` bash
6
+ $ zig run basic.zig
7
+ One
8
+ ```
9
+
10
+ ``` bash
11
+ $ zig run default.zig
12
+ Default
13
+ ```
14
+
15
+ ``` bash
16
+ $ zig run string.zig
17
+ 1
18
+ ```
Original file line number Diff line number Diff line change
1
+ const std = @import ("std" );
2
+ const print = std .debug .print ;
3
+
4
+ pub fn main () ! void {
5
+ var gpa = std .heap .GeneralPurposeAllocator (.{}){};
6
+ defer _ = gpa .deinit ();
7
+
8
+ const allocator = gpa .allocator ();
9
+ var map = std .StringHashMap (u8 ).init (allocator );
10
+ defer map .deinit ();
11
+
12
+ // Insert
13
+ try map .put ("Zero" , 0 );
14
+ try map .put ("One" , 1 );
15
+ try map .put ("Two" , 2 );
16
+
17
+ const value = map .get ("One" );
18
+ if (value ) | v | {
19
+ print ("{}" , .{v });
20
+ }
21
+ }
You can’t perform that action at this time.
0 commit comments