수색…


표준 사용

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];

항목은 키 종료가 제거되고 removetrue 반환하면 .remove() 를 사용하여 제거 할 수 있습니다.

assert(aa.remove("x"));

지정된 키가 존재하지 않는 경우, remove 는 아무것도 실시하지 않고 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