Untitled

Run Settings
LanguageSwift
Language Version
Run Command
// Container 协议 protocol Container { associatedtype ItemType // 添加一个新元素到容器里 mutating func append(_ item: ItemType) // 获取容器中元素的数 var count: Int { get } // 通过索引值类型为 Int 的下标检索到容器中的每一个元素 subscript(i: Int) -> ItemType { get } } // // 遵循Container协议的泛型TOS类型 struct Stack<Element>: Container { // Stack<Element> 的原始实现部分 var items = [Element]() mutating func push(_ item: Element) { items.append(item) } mutating func pop() -> Element { return items.removeLast() } // Container 协议的实现部分 mutating func append(_ item: Element) { self.push(item) } var count: Int { return items.count } subscript(i: Int) -> Element { return items[i] } } // 扩展,将 Array 当作 Container 来使用 extension Array: Container {} func allItemsMatch<C1: Container, C2: Container> (_ someContainer: C1, _ anotherContainer: C2) -> Bool where C1.ItemType == C2.ItemType, C1.ItemType: Equatable { // 检查两个容器含有相同数量的元素 if someContainer.count != anotherContainer.count { return false } // 检查每一对元素是否相等 for i in 0..<someContainer.count { if someContainer[i] != anotherContainer[i] { return false } } // 所有元素都匹配,返回 true return true } var tos = Stack<String>() tos.push("google") tos.push("W3xue") tos.push("taobao") var aos = ["google", "W3xue", "taobao"] if allItemsMatch(tos, aos) { print("匹配所有元素") } else { print("元素不匹配") }
Editor Settings
Theme
Key bindings
Full width
Lines