part 1 swap using generic function func swap
- Slides: 23
範例程式part 1 // swap using generic function func swap. Data<T>(a: inout T, b: inout T) { let temp = a a=b b = temp } var one. Int = 100 var another. Int = 200 print("Before swapped: ") print("one. Int = (one. Int), another. Int = (another. Int) ") swap. Data(a: &one. Int, b: &another. Int) print("After swapped: ") print("one. Int = (one. Int), another. Int = (another. Int) ") var one. String = "Hello" var another. String = "Swift" print("Before swapped: ") print("one. Int = (one. String), another. Int = (another. String) ") swap. Data(a: &one. String, b: &another. String) print("After swapped: ") print("one. Int = (one. String), another. Int = (another. String) ")
範例程式part 2 var one. Double = 123. 456 var another. Double = 654. 321 print("Before swapped: ") print("one. Int = (one. Double), another. Int = (another. Double) ") swap. Data(a: &one. Double, b: &another. Double) print("After swapped: ") print("one. Int = (one. Double), another. Int = (another. Double) ")
輸出結果 Before swapped: one. Int = 100, another. Int = 200 After swapped: one. Int = 200, another. Int = 100 Before swapped: one. String = Hello, another. String = Swift After swapped: one. String = Swift, another. String = Hello Before swapped: one. Int = 123. 456, another. Int = 654. 321 After swapped: one. Int = 654. 321, another. Int = 123. 456
範例程式part 1 // generic type struct Queue<T> { var items = [T]() mutating func insert(item: T) { items. append(item) } mutating func delete() -> T { return items. remove(at: 0) } } var queue. Of. Int = Queue<Int>() queue. Of. Int. insert(item: 100) queue. Of. Int. insert(item: 200) queue. Of. Int. insert(item: 300) queue. Of. Int. insert(item: 400) queue. Of. Int. insert(item: 500) print("The integer queue has following elements: ") for i in queue. Of. Int. items { print("(i) ", terminator: "") } print("")
範例程式part 2 queue. Of. Int. delete() print("After delete 100, the queue has following elements: ") for i in queue. Of. Int. items { print("(i) ", terminator: "") } print("") queue. Of. Int. insert(item: 600) print("After insert 600, the queue has following elements: ") for i in queue. Of. Int. items { print("(i) ", terminator: "") } print("n")
輸出結果 The integer queue has following elements: 100 200 300 400 500 After delete 100, the queue has following elements: 200 300 400 500 After insert 600, the queue has following elements: 200 300 400 500 600
17. 2. 1 找某一值位於陣列的何處 n以下是一找尋某一字串在字串陣列的位置,其片段程式如 下: func search. Data(array: [String], value. To. Search: String) ->Int? { } for (index, value) in array. enumerated() { if value == value. To. Search { return index } } return nil n同理也可以將上述的程式改為整數、浮點數陣列。
範例程式part 1 func search. Data<T: Equatable>(array: [T], value. To. Search : T) -> Int? { for (index, value) in array. enumerated() { if value == value. To. Search { return index } } return nil } let arrayof. Strings = ["Apple", "Guava", "Banana", "Kiwi", "Orange"] let found = search. Data(array: arrayof. Strings, value. To. Search: "Kiwi") print("The index of Kiwi is (found)") let found 2 = search. Data(array: arrayof. Strings, value. To. Search: "Pineapple") print("The index of Pineapple is (found 2)") let array. Of. Int = [11, 22, 33, 44, 55] let found 3 = search. Data(array: array. Of. Int, value. To. Search: 55) print("n. The index of 55 is (found 3)") let found 4 = search. Data(array: array. Of. Int, value. To. Search: 66) print("The index of 66 is (found 4)") let array. Of. Double = [11. 1, 22. 2, 33. 3, 44. 4, 55. 5] let found 5 = search. Data(array: array. Of. Double, value. To. Search: 22. 2)
範例程式part 2 print("n. The index of 22. 2 is (found 5)") let found 6 = search. Data(array: array. Of. Double, value. To. Search: 66. 6) print("The index of 66. 6 is (found 6)")
輸出結果 The index of Kiwi is Optional(3) The index of Pineapple is nil The index of 55 is Optional(4) The index of 66 is nil The index of 22. 2 is Optional(1) The index of 66. 6 is nil
範例程式 //sorting integer numbers var arr. Of. Int = [10, 30, 5, 7, 2, 8, 12[ print("Before sorted(" : for i in arr. Of. Int} print("(i) ", terminator("" : { func bubble. Sort(arr: inout [Int} ([ var flag: Bool for i in 0. . <arr. count-1} flag = false for j in 0. . <arr. count-i-1} if arr[j] > arr[j+1} [ flag = true let temp = arr[j[ { { arr[j] = arr[j+1[ arr[j+1] = temp if flag == false} break { { { bubble. Sort(arr: &arr. Of. Int( print("nn. After sorted(" : for j in arr. Of. Int} print("(j) ", terminator("" : { print("")
輸出結果 Before sorted: 10 30 5 7 2 8 18 12 After sorted: 2 5 7 8 10 12 18 30
範例程式part 1 protocol Extra. Information { associatedtype Item. Type var count: Int {get} subscript(i: Int) -> Item. Type {get} } struct Queue. Type<T>: Extra. Information { var items = [T]() mutating func insert(item: T) { items. append(item) } mutating func delete() { items. remove(at: 0) } //comformance to the Extra. Information Protocol typealias Item. Type = T var count: Int { return items. count } subscript(i: Int) -> T { return items[i] } }
範例程式part 2 var queue. Of. Data = Queue. Type<Int>() queue. Of. Data. insert(item: 100) queue. Of. Data. insert(item: 200) queue. Of. Data. insert(item: 300) queue. Of. Data. insert(item: 400) queue. Of. Data. insert(item: 500) print("陣列中有(queue. Of. Data. count)個元素") for i in queue. Of. Data. items { print("(i) ", terminator: "") } print("n") queue. Of. Data. insert(item: 600) print("加入 600後,陣列中有(queue. Of. Data. count)個元素") for i in queue. Of. Data. items { print("(i) ", terminator: "") } print("n") queue. Of. Data. delete() print("queue. Of. Data[2] = (queue. Of. Data[2])") print("刪除 100後,陣列中有(queue. Of. Data. count)個元素") print("")
輸出結果 陣列中有5個元素 100 200 300 400 500 刪除 100後,陣列中有4個元素 200 300 400 500 加入 600後,陣列中有5個元素 200 300 400 500 600 queue. Of. Data[2] = 400
- System.collections.generics
- Void func(int num)
- Template class t t func(t a)
- Delegate action func predicate c#
- Swapping
- Swap out swap in
- Swap out swap in
- @jesus david pineda j:men. swap part 1
- Using system collections generic
- How to factor with generic rectangle
- Difference between c and c++
- Fdfdfdfd pointer
- Consider the following c function void swap
- Six generic administrative functions
- Addition symbol
- Part to part ratio definition
- Brainpop ratios
- What is technical description
- 3 parts of bar
- The phase of the moon you see depends on ______.
- Part to part variation
- Special factor
- Defrost using internal heat is accomplished using
- Function of seed