0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Swift 配列要素の削除、remove,drop,pop系の違いをソースで見る

Posted at

paiza問題集で配列の要素削除をやっているときに見かけたremoveとdropとpop
それぞれどんな違いがあるのか知りたい。

対象読者

Swift初学者の方

ググっても出てこないので公式をみにいく

dropFirst

Declaration
func dropFirst(_ k: Int = 1) -> Self.SubSequence
Discussion:コレクション空でも許す
let numbers = [1, 2, 3, 4, 5]
print(numbers.dropFirst(2))
// Prints "[3, 4, 5]"
print(numbers.dropFirst(10))
// Prints "[]" 
Complexity
O(1) if the collection conforms to ; otherwise, O(k), 
where k is the number of elements to drop from the beginning of the collection.RandomAccessCollection
O(1)に適合する場合O(1)、そうでない場合O(k), 
kはコレクションの先頭から削除する要素の数である.
See Also
Excluding Elements
func dropLast(Int) -> Self.SubSequence
Returns a subsequence containing all but the specified number of final elements.
func drop(while: (Self.Element) throws -> Bool) rethrows -> Self.SubSequence
Returns a subsequence by skipping elements while returns and returning the remaining elements.predicatetrue

removeFirst

Declaration
@discardableResult mutating func removeFirst() -> Self.Element
Discussion:空は許さない
var bugs = ["Aphid", "Bumblebee", "Cicada", "Damselfly", "Earwig"]
bugs.removeFirst()
print(bugs)
// Prints "["Bumblebee", "Cicada", "Damselfly", "Earwig"]" 
// Calling this method may invalidate any existing indices for use with this collection.
// このメソッドを呼び出すと、このコレクションで使用するための既存のインデックスが無効になることがあります。
Complexity
O(n), where n is the length of the collection.
O(n)、nはコレクションの長さである。
See Also
Removing Elements
func remove(at: Int) -> Element
Removes and returns the element at the specified position.
func removeFirst(Int)
Removes the specified number of elements from the beginning of the collection.
func removeLast() -> Self.Element
Removes and returns the last element of the collection.
Available when Self conforms to BidirectionalCollection.
func removeLast(Int)
Removes the specified number of elements from the end of the collection.
Available when Self conforms to BidirectionalCollection.
func removeSubrange(Range<Self.Index>)
Removes the elements in the specified subrange from the collection.
func removeSubrange<R>(R)
Removes the elements in the specified subrange from the collection.
func removeAll(where: (Self.Element) throws -> Bool) rethrows
Removes all the elements that satisfy the given predicate.
func removeAll(keepingCapacity: Bool)
Removes all elements from the array.
func popLast() -> Self.Element?
Removes and returns the last element of the collection.
Available when Self conforms to BidirectionalCollection.

わかること

  • 計算量が異なること
  • コレクション空の許可有無
  • popLastとremoveは同じElement
  • dropはSubSequence
  • dropLastは空を許可する
  • removeLastは空を許可しない
  • popLastは空を許可する
  • remove系はindex依存に注意する必要あり

わからないこと

  • SubSequenceってどんなの→Slice構造体の一片副次的配列
  • Elementってどんなの→collectionの要素?
  • Sliceってなに→collection含む構造体

次やること

おわりに

1次ソースを見にいく練習。
ググって出てくるブログではなく公式英文や英語版のStackOverflowを読めるようにする。
2年間Swiftを学んできたがSliceを初めて目にした。
わからないこと、わかったことを明文化してすっきりしたい。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?