1
我正在構建數組的關聯數組。我嘗試使用appender,但獲得segfault。什麼是正確的方法來做到這一點?下面是小測試程序:Dlang數組關聯數組
import std.stdio;
import std.array;
struct Entry {
string ip;
string service;
}
void main(string[] args) {
Entry[3] ents;
ents[0] = Entry("1.1.1.1", "host1");
ents[1] = Entry("1.1.1.2", "host2");
ents[2] = Entry("1.1.1.1", "dns");
string[][string] ip_hosts;
foreach (entry; ents) {
string ip = entry.ip;
string service = entry.service;
string[] *new_ip = (ip in ip_hosts);
if (new_ip !is null) {
*new_ip = [];
}
auto app = appender(*new_ip);
app.put(service);
continue;
}
writeln("Out:", ip_hosts);
}
我想,這可能與使用指針與附加目的地名單的事,但我不知道。有人知道什麼是錯的,並且是解決問題的好方法嗎?
謝謝,我現在只是完全避開了appender。 –