D Language
सहयोगी एरे
खोज…
मानक उपयोग
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()
, अगर कुंजी रास्ते निकाल दिया जाएगा और remove
देता है true
:
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