HashSet 的 put/iterator/remove 函数

此用例展示了 HashSet 的基本使用方法。

代码如下:

import std.collection.* /* 测试 */ main() { var set: HashSet<String> = HashSet<String>() // set: [] set.put("apple") // set: ["apple"] set.put("banana") // set: ["apple", "banana"], not in order set.put("orange") // set: ["apple", "banana", "orange"], not in order set.put("peach") // set: ["apple", "banana", "orange", "peach"], not in order var itset = set.iterator() while(true) { var value = itset.next() match(value) { case Some(v) => if (!set.contains(v)) { print("Operation failed") return 1 } else { println(v) } case None => break } } set.remove("apple") // set: ["banana", "orange", "peach"], not in order println(set) return 0 }

由于 Set 中的顺序不是固定的,因此运行结果可能如下:

apple banana orange peach [banana, orange, peach]