μ°Έκ³ μ¬μ΄νΈ:
The Swift Language Guide
Apple Developer
@frozen struct Set<Element> where Element : Hashable
// λμΌ νν
var ingredients: Set<String> = Set<String>()
var ingredients = Set<String>()
var ingredients: Set<String> = ["cocoa beans","sugar", "cocoa butter", "salt"]
var ingredients: Set = ["cocoa beans","sugar", "cocoa butter", "salt"]
print("I mixed \(ingredients.count) ingredients")
// I mixed 4 ingredients
if ingredients.isEmpty{
print("No ingredients")
} else{
print("I have ingredients")
}
// "I have ingredients"
if ingredients.contains("sugar"){
print("No thanks, too sweet.")
} else {
print("It's a delcious food")
}
// No thanks, too sweet.
ingredients.insert("water")
// ["water", "sugar", "cocoa beans", "cocoa butter", "salt"]
ingredients.remove("cocoa beans")
// ["cocoa butter", "sugar", "water", "salt"]
for ingredient in ingredients{
print(ingredient)
}
//alt
//water
//sugar
//same
let someNumbersA: Set = [1,3,4,8,10]
let someNumbersB: Set = [1,3,4,8,10]
if someNumbersA==someNumbersB{
print("same")
} else {
print("different")
}
// Prints "same"
var primes: Set = [2, 3, 5, 7]
// Tests whether primes is a subset of a Range<Int>
print(primes.isSubset(of: 0..<10))
// Prints "true"
// Performs an intersection with an Array<Int>
let favoriteNumbers = [5, 7, 15, 21]
print(primes.intersection(favoriteNumbers))
// Prints "[5, 7]"
let students: Set = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]
let sortedStudents = students.sorted()
print(sortedStudents)
// ["Abena", "Akosua", "Kofi", "Kweku", "Peter"]
let decendingStudents = students.sorted(by: >)
print(decendingStudents)
// ["Peter", "Kweku", "Kofi", "Akosua", "Abena"]
μ°Έκ³ λ‘ μ΄ Sorting Algorithmμ νμ Stableμνλ₯Ό 보μ₯νμ§ μλλ€κ³ νλ€.
(Swiftμμ μ΄λ€ μ λ ¬ μκ³ λ¦¬μ¦μ μ¬μ©νλμ§ νμΈν΄μΌ ν λ―)
μλ¬΄νΌ μκ° λ³΅μ‘λλ (nlogn) nμ Setμ κΈΈμ΄μ΄λ€.