美文网首页
Swift_集合 let inde = numbers.part

Swift_集合 let inde = numbers.part

作者: Eyes_cc | 来源:发表于2020-12-18 17:23 被阅读0次

对集合的元素进行重新排序(改变原数组),不满足给定条件的在前面,满足的在后面,返回索引' p '

/// 对集合的元素进行重新排序(改变原数组),不满足给定条件的在前面,满足的在后面,返回索引' p '。
///
/// 在对一个集合进行分区之后,存在一个主索引' p ',其中' p '之前的所有元素都不满足给定条件' ,' p '及' p '之后的所有元素都满足'。
///
/// 在下面的示例中,通过匹配给定条件(大于30的元素)对数字数组进行分区。
///
///
/// - Returns: 返回重新排序后的 ' p ' 索引。如果集合中没有元素匹配' 给定条件 ',则返回的索引等于集合的' endIndex '。
///
/// - Complexity: O(*n*), 集合的长度.
    @inlinable public mutating func partition(by belongsInSecondPartition: (Element) throws -> Bool) rethrows -> Int

var numbers = [30, 40, 20, 30, 30, 60, 10]

let p = numbers.partition(by: { $0 > 30 })
print(p)       // 5

let first = numbers[..<p]
print(first)   // [30, 10, 20, 30, 30]

let second = numbers[p...]
print(second)  // [60, 40]

print(numbers) // [30, 10, 20, 30, 30, 60, 40]

相关文章

网友评论

      本文标题:Swift_集合 let inde = numbers.part

      本文链接:https://www.haomeiwen.com/subject/mphfnktx.html