HashMap ve AutoHashMap Kullanımı
std üzerinden anahtar-değer depolama.
HashMap ve AutoHashMap Kullanımı, CoddyKit'te ücretsiz bir Zig Academy dersidir. Bu, 4 dersinin 3. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Zig Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Zig Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
Why a Hash Map
A hash map stores key-value pairs and looks them up in near-constant time. Zig ships one in the standard library, so you rarely write your own. 🗺️
Reach for AutoHashMap First
std.AutoHashMap picks hashing and equality automatically for common key types like integers and enums. Give it a key type and a value type.
const Map = std.AutoHashMap(u32, []const u8);It Needs an Allocator
Like other growable containers, the map borrows memory. Pass an allocator to init and the map grows as you add entries.
var map = std.AutoHashMap(u32, u32).init(allocator);
defer map.deinit();Insert with put
put adds or overwrites a key. It can allocate while growing, so it returns an error union and you use try.
try map.put(1, 100);Look Up with get
get returns an optional value: the stored value if the key exists, or null otherwise. No crash on a missing key.
const v = map.get(1); // ?u32Unwrap the Result
Because get is optional, handle the absent case with orelse or an if capture before using the value.
const v = map.get(1) orelse 0;Check and Remove
Ask contains for a yes-or-no answer, and call remove to delete a key. Remove returns true if the key was present.
if (map.contains(1)) _ = map.remove(1);When Auto Cannot Decide
For keys like slices, automatic hashing will not compile. Then you use the lower-level std.HashMap and supply a context.
StringHashMap for Text Keys
String keys are common, so std offers a ready helper: std.StringHashMap already knows how to hash a []const u8.
var m = std.StringHashMap(i32).init(allocator);
try m.put("score", 42);Iterate Over Entries
Get an iterator and loop to visit every pair. Each entry exposes a key pointer and a value pointer.
var it = map.iterator();
while (it.next()) |e| {
std.debug.print("{d}={d}\n", .{ e.key_ptr.*, e.value_ptr.* });
}Always deinit the Map
The map owns its internal buffers, so call deinit when you are done. The testing allocator flags it if you forget.
defer map.deinit();Quick Check
You need a hash map whose keys are integers. Which standard type is the simplest fit?
Recap
Use AutoHashMap for simple keys and StringHashMap for text. Init with an allocator, put and get pairs, iterate, then deinit. 🎯
Sıkça Sorulan Sorular
“HashMap ve AutoHashMap Kullanımı” dersi ücretsiz mi?
Evet — “HashMap ve AutoHashMap Kullanımı” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Zig Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Zig Academy kursu toplamda 4 dersten oluşur.
“HashMap ve AutoHashMap Kullanımı” dersinde ne öğreneceğim?
std üzerinden anahtar-değer depolama. Zig Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Zig Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Zig Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 3. dersidir.
“HashMap ve AutoHashMap Kullanımı” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Zig Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Zig Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Sıfırdan Genel Amaçlı Yığın
- Tek Bağlantılı Liste
- HashMap ve AutoHashMap Kullanımı
- Profil Oluşturma ve Güvenlik Dengeleri