サーチ…
標準使用
int[string] wordCount(string[] wordList) {
int[string] words;
foreach (word; wordList) {
words[word]++;
}
return words;
}
void main() {
int[string] count = wordCount(["hello", "world", "I", "say", "hello"]);
foreach (key; count.keys) {
writefln("%s: %s", key, count[key]);
}
// hello: 2
// world: 1
// I: 1
// say: 1
// note: the order in the foreach is unspecified
}
リテラル
int[string] aa0 = ["x": 5, "y": 6]; //int values, string keys
auto aa1 = ["x": 5.0, "y": 6.0]; // double values, string keys
string[int] aa2 = [10: "A", 11: "B"]; //string values, int keys
キーと値のペアを追加する
int[string] aa = ["x": 5, "y": 6];
// The value can be set by its key:
aa["x"] = 7;
assert(aa["x"] == 7);
// if the key does not exist will be added
aa["z"] = 8;
assert(aa["z"] == 8);
キーと値のペアを削除する
連想配列をaa
としましょう:
int[string] aa = ["x": 5, "y": 6];
キーの終了が削除され、 remove
がtrue
remove
返した場合、 .remove()
を使用して項目を削除できtrue
。
assert(aa.remove("x"));
指定されたキーが存在しない場合、 remove
は何もせずfalse
を返しfalse
。
assert(!aa.remove("z"));
キーが存在するかどうかを確認する
int[string] numbers = ["a" : 10, "b" : 20];
assert("a" in numbers);
assert("b" in numbers);
assert("c" in numbers);
Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow