在快速演算法中,如何獲取分塊集合的第二項?
let a = ["a", "b", "c", "d", "e"]
let chunked = a.chunks(ofCount: 2) //["a", "b"] ["c", "d"] ["e"]
現在我想得到 ["c", "d"]
我可以使用
chunked.index(chunked.startIndex, offsetBy: 1) //Index(baseRange: Range(2..<4))
但在那之后,我不知道如何得到確切的結果。
請幫忙!
uj5u.com熱心網友回復:
你已經得到了索引。只需使用下標語法來訪問集合:
chunked[chunked.index(chunked.startIndex, offsetBy: 1)]
這會給你一個ArraySlice<String>. 列印這個會給你["c", "d"]。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/409387.html
標籤:
上一篇:理解Harvey&vanderHoeven2019演算法(大整數乘法)
下一篇:在單個串列中比較字串的最有效方法
