में किसी सरणी से किसी दिए गए मान की सभी घटनाओं को हटाकर मान लीजिए कि मेरे पास एक सरणी है। मैं उस सरणी के भीतर सभी तत्वों को हटाना चाहता हूं जिनके पास दिया गया मान है। क्या किसी को भी यह करना आता है? जिस मूल्य को मैं निकालने का प्रयास कर रहा हूं वह एक से अधिक बार हो सकता है और सरणी जरूरी नहीं है। मैं एक नई सरणी बनाने के बजाय सरणी को जगह में फ़िल्टर करना पसंद करूंगा। उदाहरण के लिए, [1, 2, 3, 2, 4]
से 2
मान को हटाकर [1, 3, 4]
परिणाम देना चाहिए।डी
T[] without(T)(T[] stuff, T thingToExclude) {
auto length = stuff.length;
T[] result;
foreach (thing; stuff) {
if (thing != thingToExclude) {
result ~= thing;
}
}
return result;
}
stuff = stuff.without(thingToExclude);
writeln(stuff);
यह अनावश्यक रूप से जटिल और अक्षम लगता है:
यह सबसे अच्छी बात मैं के साथ आ सकता है। क्या कोई आसान तरीका है? मैंने मानक लाइब्रेरी में std.algorithm मॉड्यूल को देखा जो कुछ उपयोगी खोजने की उम्मीद कर रहा था, लेकिन जो कुछ भी दिखता था वह वही करेगा जो मैं चाहता था वह समस्याग्रस्त था।
import std.stdio, std.algorithm, std.conv;
auto stuff = [1, 2, 3, 2, 4];
auto thingToExclude = 2;
/* Works fine with a hard-coded constant but compiler throws an error when
given a value unknowable by the compiler:
variable thingToExclude cannot be read at compile time */
stuff = filter!("a != " ~ to!string(thingToExclude))(stuff);
writeln(stuff);
/* Works fine if I pass the result directly to writeln but compiler throws
an error if I try assigning it to a variable such as stuff:
cannot implicitly convert expression (filter(stuff)) of type FilterResult!(__lambda2,int[]) to int[] */
stuff = filter!((a) { return a != thingToExclude; })(stuff);
writeln(stuff);
/* Mysterious error from compiler:
template to(A...) if (!isRawStaticArray!(A)) cannot be sliced with [] */
stuff = to!int[](filter!((a) { return a != thingToExclude; })(stuff));
writeln(stuff);
तो, मैं कैसे एक मूल्य की सभी घटनाओं की एक सरणी से अनुक्रमित उन्हें प्रदर्शित करने वाले को जानने के बिना निकाल सकते हैं: यहाँ सब मैंने कोशिश की है कि काम नहीं किया के कुछ उदाहरण हैं?
आपके अंतिम प्रयास के साथ समस्या ऑपरेटर प्राथमिकता है: 'to! Int []' '(टी)' टेम्पलेट को चालू कर रहा है और फिर '[] ', यानी स्लाइस ऑपरेटर को लागू कर रहा है। यदि आप लक्ष्य प्रकार के हिस्से के रूप में ब्रैकेट चाहते हैं तो आपको माता-पिता की आवश्यकता है: 'to! (Int []) (...)'। यह अभी भी काम नहीं करेगा, लेकिन यह अर्थात् सही है। – scry